jQuery 会话超时是否在每个请求上重置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10370110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:11:59  来源:igfitidea点击:

Does Session timeout reset on every request

jqueryasp.netajaxsessionasp.net-ajax

提问by Arian

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,...

Ajax 请求是否会导致重置会话超时?像Update PaneljQuery ajax...

thanks

谢谢



Edit 1)

编辑 1)

Does HTTP Getcause resetting session timeout??

是否HTTP Get会导致重置会话超时??

采纳答案by J. Ed

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

是的,它确实。无论您是否实际使用,都Session没有关系。
但是,如果您使用ajax 调用,您可能会遇到一些问题。
(虽然我自己没遇到过,这里解释一下)

回答by Pankaj

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

无论我们是否检查会话变量,会话超时是否会在每个请求上重置?

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

Session如果您继续调用服务器端代码,则不会过期。该session超时将被重新对每个请求到服务器。在对同一网站的后续请求中,浏览器提供ASP.NET_SessionId Cookie服务器端模块用于访问会话值(如用户信息)的 。

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

enter image description here

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

问题 - 2- Ajax 请求是否会导致重置会话超时?像更新面板,jQuery ajax,...

问题 - 3- HTTP Get 是否会导致重置会话超时?

Sessionwill expire in case user waited too long between requests. Sessionwill not expire if you keep on calling server sidecode. The sessiontime out will be be resetting on each request to server

Session如果用户在请求之间等待时间过长,则会过期。Session如果您继续调用server side代码,则不会过期。该session超时将在每个请求,服务器被重置

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" />

回答by Arcturus

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.

在建立会话变量之前,每次回发都会生成一个新的会话 ID。

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

Ajax 请求是否会导致重置会话超时?像更新面板,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.

默认情况下保存在 cookie 中的会话 ID,为每个 AJAX 以及非 AJAX 请求发送。因此服务器知道会话用户是活动的。不要相信我的话。在 ie 中使用 fiddler 或 F12 工具。您可以看到随每个 AJAX GET/POST 请求发送的 cookie。

回答by done_merson

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

这将取决于很多因素,因此我建议您进行如下测试。真正需要不到 5 分钟的时间才能确定您的确切情况是否适用于您的环境。这是我用来测试的代码,我使用 Telerik 控件来测试这个想法,但我添加了一个 .get 以获得您想要的确切答案。aspx页面有

<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

.cs 页面有

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.

---您可以根据自己的情况安排上述内容。但这个想法很简单。你加载页面。单击“续订”按钮,这将设置您的会话。等待 61 秒,页面将刷新并且会话变量消失。再试一次,但这次在几秒钟后单击其他两个按钮之一,当页面刷新时,您将看到会话变量从 ajax 调用中保持不变。这种情况下的ajax调用会刷新会话变量。