[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.NET server side access to page <% tags

Posted on 2009-02-13
5
Medium Priority
?
427 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:georgeb3
  • 3
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 1000 total points
ID: 23632366
Do you assign a value to that variable in the page load event ? You just need to declare the variable as public in the code-behind.
0
 
LVL 1

Author Comment

by:georgeb3
ID: 23632389
Interesting...I'll give that a try.
0
 
LVL 1

Author Comment

by:georgeb3
ID: 23632498
jpaulino,

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

Thanks again!
0
 
LVL 1

Author Closing Comment

by:georgeb3
ID: 31546554
Thanks!
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 23632513
Glad I could help!
jpaulino
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month19 days, 20 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question