Link to home
Start Free TrialLog in
Avatar of georgeb3
georgeb3Flag for United States of America

asked on

ASP.NET server side access to page <% tags

Hello,

I have used vb with my ASP.net pages and generally they are setup with a code-behind page (code not on same page as html) (yes we are still using .net v1.1)

However, I received a C# page from another department and need to include it in my project. Problem isn't converting C# to vb.net...that's not hard. Problem is that the one page contains the html and code and they do some things that I'm not familiar with when using a code-behind setup.

For one, they change the page title in the <HEAD> section...how do you do that when all of the tags need to be in the <FORM> tags in a code-behind setup?

Can I have one page like this when all other pages in the project are setup for the code-behind setup?

I know I can use literal controls for most of what they show using the old style <% ... %> tags...but is there a way to continue using the <% ... %> tags and have the variables within be assignable from the server-side code? For example, in the case of:

<% if ( configuredProperly ) { %>
You may logout at <a href="BLAH BLAH">BLAH BLAH</a>.
<% } %>

Is there anyway that the configuredProperly boolean can still be resolvedeven though the code has beenmoved to the code-behind page?

Thanks,

George


<%@ Page Language="C#" Debug="true" %>
<%@ assembly name="System.DirectoryServices, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>
<%@ import Namespace="System.DirectoryServices" %>
<%@ import Namespace="System.Text.RegularExpressions" %>
<script runat="server">
 
/*
 Change ldapUsername and ldapPassword to match
 your registered Kerberos principal and password
*/
string ldapUsername = "";
string ldapPassword = "";
 
// DO NOT MAKE CHANGES BELOW THIS LINE
bool configuredProperly = false;
string pageTitle;
string pennKey = "UNAVAILABLE";
string pennId = "UNAVAILABLE";
 
void Page_Load(Object sender, EventArgs args) {
 
	if ( !isNullOrEmpty(Request.Headers["REMOTE_USER"]) && !isNullOrEmpty(Request.Headers["COSIGN_SERVICE"]) ) {
		configuredProperly = true;
 
		pageTitle = "IISCosign Is Configured Properly.";
		pennKey = Request.Headers["REMOTE_USER"];
 
		if ( !Regex.IsMatch(pennKey, @"^[A-Za-z][A-Za-z0-9]{1,7}$") ) {
			pennId = "[Test skipped. PennKey unsafe for use in LDAP query.]";
			return;
		}
 
		if ( isNullOrEmpty (ldapUsername) || isNullOrEmpty(ldapPassword) ) {
			pennId = "[Test skipped. LDAP credentials not set.]";
			return;
		}
 
		try {
			DirectoryEntry entry = new DirectoryEntry("LDAP://BLAH BLAH");
			entry.AuthenticationType = AuthenticationTypes.Encryption;
			entry.Username = ldapUsername;
			entry.Password = ldapPassword;
 
			DirectorySearcher searcher = new DirectorySearcher(entry);
			searcher.Filter = "pennname=" + Request.Headers["REMOTE_USER"];
			SearchResult result = searcher.FindOne();
 
			if (result != null ) {
				ResultPropertyCollection coll = result.Properties;
				pennId = result.Properties["pennid"][0].ToString();
			}
		}
		catch (Exception e) {
			pennId = "[Exception occurred during retrieval. LDAP username and password likely unset.]";
		}
	}
	else {
		pageTitle = "IISCosign Is MISCONFIGURED.";
	}
 
}
 
bool isNullOrEmpty ( string str ) {
	return (str == null || str.Length == 0);
}
</script>
<html>
<head>
<title><%= pageTitle %></title>
</head>
<body>
<h2><%= pageTitle %></h2>
<p>
<%
	Response.Write("PennKey: " + Server.HtmlEncode( pennKey ) + "<br/>");
	Response.Write("PennID: "  + Server.HtmlEncode( pennId  ) );
%>
</p>
 
<% if ( configuredProperly ) { %>
You may logout at <a href="BLAH BLAH">BLAH BLAH</a>.
<% } %>
 
<p>
<table border="1">
<thead>
<tr>
<th>Header</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<%
foreach ( string headerKey in Request.Headers ) {
	Response.Write("<tr>");
	Response.Write("<td>" + Server.HtmlEncode( headerKey                  ) + "</td>");
	Response.Write("<td>" + Server.HtmlEncode( Request.Headers[headerKey] ) + "</td>");
	Response.Write("</tr>");
}
%>
</tbody>
</table>
</p>
 
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of georgeb3

ASKER

Interesting...I'll give that a try.
jpaulino,

that worked like a charm...can't believe I was so stupid.

Thanks again!
Thanks!
Glad I could help!
jpaulino