简体   繁体   English

会话到期前提醒用户,可以选择续订会话

[英]Alert user before session expires, option to renew session

I am setting session time in web config. 我在Web配置中设置会话时间。 I also have a javascript for detecting session timeout and letting the user know. 我也有一个JavaScript,用于检测会话超时并通知用户。

I would like to be able to alert the user 30 sec or a min or more before session timeout with a popup and to give them the opportunity to extend it or cancel and just let it timeout. 我希望能够在会话超时前的30秒或更长时间提醒用户,并弹出一个窗口,让他们有机会扩展或取消它,让它超时。 Can this be done? 能做到吗? I have tried to use various methods on the internet but none seem to work. 我试图在互联网上使用各种方法,但似乎都没有用。 They all require that I download something and I don't want to have to do that. 他们都要求我下载一些东西,而我不想这样做。

<script type="text/javascript">
    var sessionTimeout = "<%= Session.Timeout %>";

    function DisplaySessionTimeout() {
        sessionTimeout = sessionTimeout - 1;

        if (sessionTimeout >= 0)
            window.setTimeout("DisplaySessionTimeout()", 60000);
        else
            alert("Your current Session is over due to inactivity.");
    }
</script>

I am also checking for timeout in code behind and rediecting but thinking about just doing it in javascript. 我还在后面的代码中检查超时并重新连接,但考虑仅在javascript中进行操作。

protected void Page_Init(object sender, EventArgs e)
{
    CheckSession();
}


private void CheckSession()
{
    if (Session["ASP.NET_SessionId"] == null)
    {
       // Session.RemoveAll();
       Response.Redirect("Home.aspx");
    }
}

You have to essentialy have a timer at the same time limit as session; 您必须在与会话相同的时间限制上设置一个计时器; there is no way for the client to know exactly the time the session is at before expiring. 客户端无法知道会话到期之前的确切时间。 That' what I've done in an application. 这就是我在应用程序中所做的。

We do a JQuery $.get("keepalive.aspx") to make a request to an ASP.NET page, which accesses session and refreshes it, keeping the current session alive. 我们执行一个JQuery $.get("keepalive.aspx")向ASP.NET页面发出请求,该页面访问会话并刷新它,从而使当前会话保持活动状态。 You'd also then reset your client-side timer. 然后,您还需要重置客户端计时器。 This has worked well for us. 这对我们来说很好。

See this post for more information. 有关更多信息,请参见此帖子

Here is a good example of how to give a "Session Timeout" warning with Jquery 是一个很好的示例,说明如何使用Jquery发出“会话超时”警告

Here is a demo of it in action 是一个实际的演示

您还可以检查一个Microsoft示例,它看起来确实是您的问题,请在此处尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM