G
Guest
When a user accesses our site, I would like to authenticate them and redirect
them to the login page if they are not authenticated. Problem is that the
method I am using in global runs more than once and the line where I check
the session username returns an error the second time through. The error is:
Object reference not set to an instance of an object. This code works in a
VS2003 project we have. Has it changed? Is there a better way to check to see
if a user is authenticated than what I am using? Thank you.
protected void Application_AcquireRequestState(Object sender, EventArgs e)
{
bool authenticationRequired = true;
string filePath = string.Empty;
if (HttpContext.Current.Session["Username"] != null) //Error here on second
iteration
{
filePath = HttpContext.Current.Request.FilePath.ToLower();
foreach (string pageFile in PagesThatDoNotRequireLogin)
{
if (filePath.LastIndexOf(pageFile) >= 0)
{
authenticationRequired = false;
break;
}
}
if (authenticationRequired)
{
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/Index.aspx", true);
}
}
}
them to the login page if they are not authenticated. Problem is that the
method I am using in global runs more than once and the line where I check
the session username returns an error the second time through. The error is:
Object reference not set to an instance of an object. This code works in a
VS2003 project we have. Has it changed? Is there a better way to check to see
if a user is authenticated than what I am using? Thank you.
protected void Application_AcquireRequestState(Object sender, EventArgs e)
{
bool authenticationRequired = true;
string filePath = string.Empty;
if (HttpContext.Current.Session["Username"] != null) //Error here on second
iteration
{
filePath = HttpContext.Current.Request.FilePath.ToLower();
foreach (string pageFile in PagesThatDoNotRequireLogin)
{
if (filePath.LastIndexOf(pageFile) >= 0)
{
authenticationRequired = false;
break;
}
}
if (authenticationRequired)
{
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/Index.aspx", true);
}
}
}