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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9753351/
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-10-26 07:44:04  来源:igfitidea点击:

Alert user before session expires, option to renew session

c#javascriptasp.net

提问by Brian Mains

I am setting session time in web config. 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. 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.

我希望能够在会话超时前 30 秒或一分钟或更长时间通过弹出窗口提醒用户,并让他们有机会延长或取消并让它超时。这能做到吗?我曾尝试在互联网上使用各种方法,但似乎都不起作用。他们都要求我下载一些东西,我不想这样做。

<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");
    }
}

回答by Brian Mains

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. You'd also then reset your client-side timer. This has worked well for us.

我们使用 JQuery$.get("keepalive.aspx")向 ASP.NET 页面发出请求,该页面访问会话并刷新它,保持当前会话处于活动状态。然后,您还需要重置客户端计时器。这对我们来说效果很好。

See this postfor more information.

有关更多信息,请参阅此帖子

回答by Coltech

Hereis a good example of how to give a "Session Timeout" warning with Jquery

是一个很好的示例,说明如何使用 Jquery 发出“会话超时”警告

Hereis a demo of it in action

是它的实际演示

回答by apros

You also can check a Microsoft example and it seems exact your issue, please try here

您还可以查看 Microsoft 示例,这似乎正是您的问题,请在此处尝试