21

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??

2
  • I raised this question:stackoverflow.com/questions/10432428/…
    – Dan An
    May 4, 2012 at 9:28
  • 4
    Use fiddler or f12 within ie. You will see that all the cookies (including the session cookie) are sent even AJAX calls. So the server is aware of the activity. Long story short, ajax or no ajax, session gets extended with user activity.
    – Arcturus
    May 8, 2012 at 21:10

4 Answers 4

21
+50

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" />
5
  • 2
    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
    – DooDoo
    May 3, 2012 at 18:42
  • @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.
    – Pankaj
    May 3, 2012 at 18:55
  • Are you sessions being CookieLess?
    – DooDoo
    May 3, 2012 at 18:59
  • 1
    I think because Cookieless=true your session not expires
    – DooDoo
    May 4, 2012 at 15:33
  • 1
    Yes. Otherwise, it also starts new session. Sorry, I was in the impression that the query was in context of session with cookies.
    – Pankaj
    May 5, 2012 at 18:24
13

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)

3
  • 1
    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.
    – J. Ed
    Apr 29, 2012 at 10:57
  • 4
    Even if you use only ajax calls, there is no problem.
    – Arcturus
    May 8, 2012 at 21:05
  • 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... Feb 1, 2022 at 19:55
4

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.

0
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.