Session in Global.asax

Last post 07-03-2009 10:24 AM by deepthoughts. 5 replies.

Sort Posts:

  • Session in Global.asax

    07-02-2009, 11:01 AM
    • Member
      9 point Member
    • thuyaaung7
    • Member since 12-17-2007, 2:28 PM
    • Posts 38

    Hi All,

    I have a question.

    I need to know that when a request with expired session id come in to a web application, how does the asp.net know it is expired?

    And in which method, it is being checked.

  • Re: Session in Global.asax

    07-02-2009, 1:12 PM

     Hi,

    override protected void OnInit(EventArgs e)
    {
    base.OnInit(e);


    //check to see if the Session is null (doesnt exist)
    if (Context.Session != null)
    {
    //check the IsNewSession value, this will tell us if the session has been reset
    if (Session.IsNewSession)
    {
    //now we know it's a new session, so we check to see if a cookie is present
    string cookie = Request.Headers["Cookie"];
    //now we determine if there is a cookie does it contains what we're looking for
    if ((null != cookie) && (cookie.IndexOf("ASP.NET_SessionId") >= 0))
    {
    //since it's a new session but a ASP.Net cookie exist we know
    //the session has expired so we need to redirect them
    Response.Redirect("YourURL");
    }
    }
    }
    }

    http://www.dotnetspider.com/resources/15837-check-for-session-time-out.aspx

    http://www.bigresource.com/ASP-how-we-know-that-the-Session-is-expired-in-asp--ojnyxftS.html

    Thanks :)

     

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

    Srinivas Kotra.


  • Re: Session in Global.asax

    07-02-2009, 2:22 PM
    • All-Star
      25,266 point All-Star
    • budugu
    • Member since 01-12-2006, 7:15 PM
    • North Carolina
    • Posts 3,773

    In Page_Load, check Session.IsNewSession value. If it's true, then session is expired.

    if (Session.IsNewSession)
    Server.Transfer("Login.aspx");


    And also read this..

    http://aspalliance.com/520_Detecting_ASPNET_Session_Timeouts.all
     

    Vijay Kodali || My Blog


    "Don't be afraid to be wrong; otherwise you'll never be right."
  • Re: Session in Global.asax

    07-02-2009, 4:01 PM
    • Contributor
      5,014 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 9:55 AM
    • Posts 739

    Hi!

    Please note that whenever a client accesses the web application, he is infact living in a session. Unless the session timeouts or you forcefully abandons it, it lives up with the client.

    Now we put up some key,value pairs in to the Session object for our own requirements.

    So, when a user with "expired" session comes, the web application is, infact, going to start a new session for the same guy. So the Session_Start event of global.asax will fire. So you can handle the logic in the global.asax page or you can put in checks in your pages like Session.IsNewSession to make sure that this request is coming from a client who is

    1.) Either new

    2.) Previously his session expired and is going to start a new session.

    Hope it helps.

    MARK AS ANSWER if it helps
  • Re: Session in Global.asax

    07-03-2009, 5:06 AM
    • Member
      9 point Member
    • thuyaaung7
    • Member since 12-17-2007, 2:28 PM
    • Posts 38

    Hi,

    First of all, Thank alot for all of your responses.

    My question was a bit unclear.

    I'd like to know how and which method, asp.net used in global.asax to identify the request session is expired and where does it generate the new session ID.

    And I also need to identify between the expired session and new user session.

    I traced the global.asax and I found that session Id has been issue between PreSendRequestContent method and BeginRequest method.

    I would like to know how it is issue and where it gets the session id from.

    Thank you guys.


  • Re: Session in Global.asax

    07-03-2009, 10:24 AM
    Answer
    • Contributor
      5,014 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 9:55 AM
    • Posts 739

    When session state is enabled (which by default is), each request for a page is checked for a SessionID value. This SessionID value is sent from the browser.

    If it is not found, then ASP.NET considers this request as a new Session and assigns the SessionID property a new unique value and sends it to the browser with thre Response.

    Where the browser keeps the SessionID? By default, its saved in "ASP.NET_SessionId" cookie. Although you can configure the application so that the SessionIDs are passed through URL and not from cookies ("cookieless" sessions).

    Please refer to the following link for more details.

    http://msdn.microsoft.com/en-us/library/ms178581.aspx

     

    Hope it helps.

    MARK AS ANSWER if it helps
Page 1 of 1 (6 items)