Hi,
I have a login application that sets a Session when the correct login is done.
I want to check this Session before each page is loaded to make sure the user is logged in.But I dont want to write the code on every page.
So I am trying to do this:
mycode...
Server.Execute("secChk.cs");
mycode...
But when i create secChk.cs as a CodeFile in VS.net and try to declare a string i get this error:
A namespace does not directly contain members such as fields or methods.
Am I able to call a .cs or does it have to be .aspx?
any ideas?
What type of file would i write the block of code to check the Session in and how do i setup that file?
Thanks, Justin
What you really should do, is look into using forms authentication instead. http://www.dotnetjunkies.com/quickstart/aspplus/doc/formsauth.aspxHi, it seems like you're trying to do stuff the old ASP classic way. In ASP.NET, you don't use Execute to execute code that's in another file.
You just call a method.
For example, your security code (which would get so much simpler if you just used Forms Authentication, as has been pointed out) could be in a method on a class that you define in this secChk.cs file, but instead of trying to execute the source file, you just compile the web site and the method becomes available by just referring to it.
Imagine your class has some static method:
public class myclass {
public static bool isOK(string something) {
// ... some code ...
}
}
Then in any page, you can just call the code by writingmynamespace.myclass.isOk("whatever");
Check this code maybe it will help.
privateconststring USER_NAME = "Name";
privatevoid Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
if(IsSessionCreated(Session["USER_NAME"]))
Response.Write("Session already created");
else
Response.Write("Session has not been created");
}
}
privatebool IsSessionCreated(object session)
{
System.Web.SessionState.HttpSessionState SessionState = (System.Web.SessionState.HttpSessionState) session;
// USER_NAME can be the private string constant variable which can hold "Name" or any other Session variable name
if(SessionState[USER_NAME] !=null)
returntrue;
else
returnfalse;
}
Thanks Bleroy,
Your answer seems to apply the most. It is true I only switched to .Net 6 months ago and am still trying to get used to it.
Thanks.
0 comments:
Post a Comment