Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a session expire during the page processing in C#

Tags:

c#

.net

asp.net

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.

like image 863
CSC Avatar asked Aug 15 '12 14:08

CSC


People also ask

Does session end when browser closed?

Nothing. Session will not expire when uses closes the browser. As http is stateless, the server is not seeing what happens on client side.

What happens when a session expires?

When the session expires, or session timeout occurs, the Session_End event in global. asax is raised (except when session is handled by the DB) and the session collection is finally cleared. If any objects are NOT holding a reference to any of values in the session collection, then GC will collect it.

When session gets expired?

If your Internet connection is unstable, periodically disconnecting and reconnecting, it can cause a website session to expire. When the Internet connection is lost the website connection can be terminated, resulting in a session expired message if you try to access any page after the Internet reconnects.

Is session expired asp net?

To preserve web server's resources, session expires after certain time of inactivity. Inactivity is considered as time between two consecutive requests from same visitor. By default, ASP.NET session will expire after 20 minutes if visitor doesn't visit any new page.


Video Answer


2 Answers

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.

like image 199
Zeph Avatar answered Oct 03 '22 15:10

Zeph


this is my code and it is working properly...

protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["id"] == null)
            {
                Session["id"] = "abc";
            }
            ViewState["id"] = Session["id"];
            Label1.Text = ViewState["id"].ToString();
            ViewState["id"] = Session["id"].ToString();
            Label1.Text += ViewState["id"].ToString();
        }

Change "session" to "Session" and "viewstate" to "ViewState"

like image 44
Ankush Jain Avatar answered Oct 03 '22 16:10

Ankush Jain