how to redirect the page when Session Expires?


I read this on http://forums.asp.net/t/1137344.aspx
when I was modifying a Global.asax file that had Response.Redirect written in its Session_End Event. I guess the programmer aimed to redirect the application to the error page like this but the fact is that u dont have the Response Object in this particular event, the reason being that it’s fired internally by the server when the session gets expired. As an alternate to that I read this post post on the above mentioned URL, it suggests that we should use a BasePage and add this code in its PageLoad event. This way the meta info will be added to all the pages that inherit from this BasePage and they will redirect after 5 seconds of the Session timeout.

I think its quite brilliant, so hats off to ya!

HI, anilperugu: The easiest way what I feel is to use Meta information and get the trick working. Consider we have a page WebPage.aspx add the below code in the the WebPage.aspx.cs file.

private void Page_Load(object sender, System.EventArgs e)
{

Response.AddHeader(“Refresh”,Convert.ToString((Session.Timeout * 60) + 5));
if(Session[“IsUserValid”].ToString()==””)
Server.Transfer(“Relogin.aspx”);

}

In the above code, The WebPage.aspx is refreshed after 5 seconds once the Session is expired. And in the page load the session is validated, as the session is no more valid. The page is redirected to the Re-Login page. Every post-back to the server will refresh the session and the same will be updated in the Meta information of the WebPage.aspx.
Best Regards,
Sincerely,
Rex Lin
Microsoft Online Community Support

Advertisements

Author: Imran Akram

A .NET/SharePoint Consultant and a political enthusiast who believes in putting the feet of the elected and/or unelected officials to fire in order to get good governance.

2 thoughts on “how to redirect the page when Session Expires?”

  1. Hello,
    Good article… but i have one question… Do u know any javascript which can validate session and redirect if its null… i have this requirement… and i cant u the above solution even if its working coz i use frame which looses its state on refresh…

    Thanks
    Sharad

    1. Hi Sharad,

      As far as I know, it should not be possible because Javascript inherently runs on the browser while the Session is in the server.
      However there is a way so you can use a meta tag for redirecting the page. http://www.netmechanic.com/news/vol2/html_no5.htm,

      I would suggest you look into your requirements and strategies all over again with a fresh outlook because right now, I think your going into a wrong direction.

      Good luck!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s