Does Session timeout reset on every request

2019-01-18 20:03发布

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

4条回答
走好不送
2楼-- · 2019-01-18 20:43

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楼-- · 2019-01-18 20:49

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.

查看更多
别忘想泡老子
4楼-- · 2019-01-18 20:58

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楼-- · 2019-01-18 21:00

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.

查看更多
登录 后发表回答