Alex Rivera | Logout

Can a session expire during the page processing in C#

Asked 2012-08-15T14:47:59.990
8

I am trying to solve a problem where the session id seems to clearing mysteriously during postback. I am sure that the value is being set and there is no other place in my code where i am clearing that session. Also, I am storing the value of the session id in my page's viewstate. During the postback the viewstate is empty which essentially means that when the value was assigned to viewstate the session variable was null. Is it possible that during code execution, session object is cleared because of timeout?

So lets say if i have following code.

if (session["id"] == null)                                        :line1
{                                                                 :line2
   session["id"] = // Generate some unique id                     :line3 
}                                                                 :line4

viewstate["id"] = session["id"];                                  :line5

is it be theoretically possible that even though session["id"] is not null in line1 it is null on line5 because of time out.

Edit
Report

1 Answer

3

I'm gonna have to say no. I just made a site and set the session timeout to 1 (minute)

<system.web>
    <compilation debug="true" targetFramework="4.0" />
  <sessionState timeout="1"></sessionState>
</system.web>

Then added a web page with this in the page load

    protected void Page_Load(object sender, EventArgs e)
    {
        Session["Test"] = "Tester";

        //Should be longer than the 1 minute session timeout
        Thread.Sleep(120001);

        Response.Write(String.Format("Session[\"Test\"] = {0}", Session["Test"]));
    }

I tested on the Cassini VS debugger, and on IIS 7 asp.net 4 and in every test the page loads with Session["Test"] = Tester. I also tried recycling the application pool manually during the sleep and got the same results.

answered 2012-08-15T18:06:12.990

Your Answer