How to check if the ASP.Net Session has expired

  1. protected void OnInit(EventArgs e)
  2.   {
  3.        base.OnInit(e);
  4.    //It appears from testing that the Request and Response both share the 
  5.    // same cookie collectionIf I set a cookie myself in the Reponse, it is 
  6.    // also immediately visible to the Request collection.  This just means that
  7.    // since the ASP.Net_SessionID is set in the Session HTTPModule (which 
  8.    // has already run), thatwe can't use our own code to see if the cookie was 
  9.    // actually sent by the agent with the request using the collection. Check if 
  10.    // the given page supports session or not (this tested as reliable indicator
  11.    // if EnableSessionState is true), should not care about a page that does 
  12.    // not need session
  13.    if (Context.Session !=null)
  14.    {
  15.    //Tested and the IsNewSession is more advanced then simply checking if 
  16.    // a cookie is present, it does take into account a session timeout, because
  17.    // I tested a timeout and it did show as a new session
  18.    if (Session.IsNewSession)
  19.     {
  20.        // If it says it is a new session, but an existing cookie exists, then it must 
  21.        // have timed out (can't use the cookie collection because even on first
  22.        // request it already contains the cookie (request and response
  23.        // seem to share the collection)
  24.      string</span> szCookieHeader <span>=</span> Request.Headers["Cookie"];
  25.     if((null!= szCookieHeader)  (szCookieHeader.IndexOf("ASP.NET_SessionId")<= 0))
  26.      {
  27.       Response.Redirect("sessionTimeout.htm");
  28.      }
  29.     }
  30.    }
  31.   }
Source

, ,

  1. No comments yet.
(will not be published)
Submit Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Subscribe to comments feed
  1. No trackbacks yet.

SetPageWidth