Does Session timeout reset on every request

28,391

Solution 1

yes, it does. it doesn't matter whether you actually use the Session or not.
However, if you're using only ajax calls, you might run into some problems.
(although I haven't encountered it myself, here's an explanation)

Solution 2

Does Session timeout reset on every request regardless of whether we check sessions variables?

Session will not expire if you keep on calling server side code. The session time out will be resetting on each request to server. On subsequent requests to the same web site, the browser supplies the ASP.NET_SessionId Cookie which the server side module uses to access session value(like user information).

---------------------------------------------------------------------------------
                     How to detect the Session TimeOut
---------------------------------------------------------------------------------

enter image description here

---------------------------------------------------------------------------------

Question - 2 - Does Ajax request cause resetting session timeout? like Update Panel ,jQuery ajax ,...

Question - 3 - Does HTTP Get cause resetting session timeout??

Session will expire in case user waited too long between requests. Session will not expire if you keep on calling server side code. The session time out will be be resetting on each request to server

Web.Config

<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" 
 sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
    cookieless="true" timeout="1" />

Solution 3

Does Session timeout reset on every request regardless of whether we check sessions variables? Or we should use atleast one session variables?

Until a session variable is established, a new session id is generated for every post back.

Does Ajax request cause resetting session timeout? like Update Panel ,jQuery ajax ,...

Session ID which is saved in the cookie by default, is sent for every AJAX as well as non AJAX request. Hence the server is aware that the session user is active. Don't take my word for it. Use fiddler or the F12 tool within ie. You can see the cookies being sent out with every AJAX GET/POST request.

Solution 4

This will depend on a lot of factors so I suggest you run a test like below. It really takes less than 5 minutes to find out if your exact situation works in your environment. Here is my code that I used to test this, I use Telerik controls to test the idea but I added a .get to get the exact answer you wanted. aspx page has

<telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
        </telerik:RadScriptManager>
        <telerik:RadAjaxManager ID="ram" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="btnFake">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="lblAnswer" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <script src="Scripts/jquery-1.4.1-vsdoc.js"></script>
    <script>
        $(document).ready(function () {
            setTimeout(function () { document.location.href = "default.aspx?next"; }, 61000);
            setInterval(function () {
                var divForTimer = $("#divTime");
                var secs = parseInt(divForTimer.html());
                secs = secs + 1;
                $("#divTime").html(secs.toString());
            }, 1000);

        });
        function getPage() {

            $.get("test.aspx", function(result) {
                $("#btnFake").val("Got it");

            });
        }
    </script>
   <asp:Label ID="lblAnswer" runat="server"></asp:Label>
   <Asp:button ID="btnTest" runat="server" Text="Renew" onclick="btnTest_Click" />
   <input type="button" ID="btnAjaxget"  onclick ="getPage()"  value="Ajax get" />
   <asp:Button ID="btnFake" runat="server"  Text ="Fake it" 
        onclick="btnFake_Click"/>
        <div id="divTime">1</div>

the .cs page has

protected void Page_Load(object sender, EventArgs e)
        {
            Session.Timeout = 1;
            if (EMSG.CommonFunctions.GetSession("test").Length > 0)
            {
                this.lblAnswer.Text = "Session=" + EMSG.CommonFunctions.GetSession("test");
            }
            else
            {
                this.lblAnswer.Text = "No session";
            }

        }

        protected void btnTest_Click(object sender, EventArgs e)
        {
            Session["test"] = "variable set";
            this.lblAnswer.Text = Session["test"].ToString();
        }

        protected void btnFake_Click(object sender, EventArgs e)
        {
            lblAnswer.Text = "Ajax called.";
        }

---you can arrange the above to fit your situation. But the idea is simple. You load the page. Click on the "Renew" button and this will set your session. Wait 61 seconds and the page will refresh and the session variable is gone. Try it again but this time click on one of the other two buttons after a few seconds and when the page refreshes you will see that the session variable has stayed intact from the ajax calls. The ajax call in this situation refreshes the session variable.

Share:
28,391
Arian
Author by

Arian

Please vote-up this thread: RDLC Report Viewer for Visual Studio 2022

Updated on July 09, 2022

Comments

  • Arian
    Arian almost 2 years

    Does Session timeout reset on every request regardless of whether we check sessions variables? Or we should use atleast one session variables?

    Does Ajax request cause resetting session timeout? like Update Panel ,jQuery ajax ,...

    thanks


    Edit 1)

    Does HTTP Get cause resetting session timeout??

  • J. Ed
    J. Ed about 12 years
    Can you elaborate what you're not sure about? as I said, I've never encountered this behavior before, so I'm not 100% myself. However, it should be no problem to build a small test webpage which sends AJAX requests, and see whether the session is kept alive or not.
  • Arian
    Arian about 12 years
    I think Session will not expire if you keep on calling server side code statement does not true.I have a simple page with an Update Panel and a button, I click on button every 2 minute and session expires after 20 minute
  • Pankaj
    Pankaj about 12 years
    @Kerezo - I have Session Timeout = 1 in my Web.config. I have same number of controls in my form. I am hitting the button after each 15 seconds and process continued till 3 minutes.
  • Arian
    Arian about 12 years
    Are you sessions being CookieLess?
  • Arian
    Arian about 12 years
    I think because Cookieless=true your session not expires
  • Pankaj
    Pankaj about 12 years
    Yes. Otherwise, it also starts new session. Sorry, I was in the impression that the query was in context of session with cookies.
  • Arcturus
    Arcturus about 12 years
    Even if you use only ajax calls, there is no problem.
  • The Red Pea
    The Red Pea over 2 years
    Does anyone have proof of sessionState timeout "sliding" with every new request? Re: AJAX calls specifically, user J.Ed provided a link ranjitkumars.wordpress.com/2010/03/02/… , which suggests AJAX calls do not slide the sessionState timeout... but Arcturus refutes that... and I don't see any official Microsoft documentation anywhere...