Friday, March 27, 2009

Catch the Session Expired case

The project I am working on requires to show a nice custom error page to the user when the session is expired. I searched a bit on the internet and came up with a good solution in forums.asp.net which I worked it out as follows:

As mentioned in the forum you need to implement an IHttpModule and redirect the context'e AcquireRequestState event to your method which goes this:

        void Application_AcquireRequestState(object sender, EventArgs e)
{
if (HttpContext.Current.Session != null && HttpContext.Current.Session.IsNewSession)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies["ASP.NET_SessionId"] != null)
{
HttpContext.Current.Session.Clear();
HttpContext.Current.Response.Redirect(
"/Errors/SessionTimeout.aspx", true);
}
}
}

}


As you can see I clear the session before redirecting to the SessionTimeout.aspx. The reason for that is that I want the user have a choice of going to login screen by providing a link in the SessionTimeout.aspx. And the login requires a session state unlike the SessionTimeout.aspx that has the attribute EnableSessionState="False" 





Friday, March 20, 2009

URL properties of Request to ASP.NET

The following attributes are some of the usefull properties of the URL object. I use the following url to send my request to localhost:


http://localhost/Test/Asghar.aspx?test=fine


And I get the following results:

AbsolutePath = /Test/Asghar.aspx
AbsoluteUri = http://localhost/Test/Asghar.aspx?test=fine
Authority = localhost
DnsSafeHost = localhost
Fragment =
Host = localhost
HostNameType= Dns
IsAbsoluteUri = True
IsFile = False
LocalPath = /Test/Asghar.aspx
OriginalString = http://localhost:80/Test/Asghar.aspx?test=fine
PathAndQuery = /Test/Asghar.aspx?test=fine
Port = 80
Query = ?test=fine
Scheme = http
UserEscaped = False
UserInfo =

And the code to do it is as followes:
    AbsolutePath = <%= System.Web.HttpContext.Current.Request.Url.AbsolutePath %><br />
AbsoluteUri = <%= System.Web.HttpContext.Current.Request.Url.AbsoluteUri %><br />
Authority = <%= System.Web.HttpContext.Current.Request.Url.Authority %><br />
DnsSafeHost = <%= System.Web.HttpContext.Current.Request.Url.DnsSafeHost %><br />
Fragment = <%= System.Web.HttpContext.Current.Request.Url.Fragment %><br />
Host = <%= System.Web.HttpContext.Current.Request.Url.Host %><br />
HostNameType= <%= System.Web.HttpContext.Current.Request.Url.HostNameType.ToString() %><br />
IsAbsoluteUri = <%= System.Web.HttpContext.Current.Request.Url.IsAbsoluteUri %><br />
IsFile = <%= System.Web.HttpContext.Current.Request.Url.IsFile %><br />
LocalPath = <%= System.Web.HttpContext.Current.Request.Url.LocalPath %><br />
OriginalString = <%= System.Web.HttpContext.Current.Request.Url.OriginalString %><br />
PathAndQuery = <%= System.Web.HttpContext.Current.Request.Url.PathAndQuery %><br />
Port = <%= System.Web.HttpContext.Current.Request.Url.Port %><br />
Query = <%= System.Web.HttpContext.Current.Request.Url.Query %><br />
Scheme = <%= System.Web.HttpContext.Current.Request.Url.Scheme %><br />
UserEscaped = <%= System.Web.HttpContext.Current.Request.Url.UserEscaped %><br />
UserInfo = <%= System.Web.HttpContext.Current.Request.Url.UserInfo %><br />
<hr />