简体   繁体   English

ASP.NET MVC2实施会话超时警告

[英]ASP.NET MVC2 Implement session timeout warning

I need help with my MVC2 app session timeout warning. 我的MVC2应用会话超时警告需要帮助。 A popup should occur when the user is within "n" minutes of the session timeout. 当用户在会话超时的“ n”分钟之内时,将出现一个弹出窗口。 Is there some way to know that information or this has to be calculated. 有什么方法可以知道该信息还是必须计算出来的信息。 I have standard get/post actions but also AJAX requests. 我有标准的获取/发布操作,也有AJAX请求。 Thanks in advance. 提前致谢。

An ASP.NET sessione expires n minutes after the last request to the server, so it's just a matter of knowing what n is, rendering it to the page, and using a simple Javascript timer to display a message when appropriate, like so: ASP.NET会话在对服务器的最后一个请求之后的n分钟后过期,因此只需知道n是什么,将其呈现到页面,并在适当的时候使用简单的Javascript计时器显示一条消息即可,如下所示:

<sctipt type="text/ecmascript">
var timeoutMins = <%= Session.Timeout %>; // HttpSessionstate.Timeout is the timeout period in minutes
setTimeout( informUser, 0.75 * ( timeoutMins * 60 * 1000 ) );
function informUser() {
    alert("Your session is expiring shortly");
}
</script>

The 0.75 * ( timeoutMins * 60 * 1000) part converts the timeout length value to miliseconds and then scales it down to give the user time to react. 0.75 * ( timeoutMins * 60 * 1000)部分将超时长度值转换为毫秒,然后按比例缩小以使用户有时间做出反应。 You could extend the session automatically by making an AJAX request to the server (with the current ASP.NET session cookie, which is important). 您可以通过向服务器发出AJAX请求来自动扩展会话(使用当前的ASP.NET会话cookie,这一点很重要)。

Modifying this code to use a fixed alert period or calculating the "minutes left" figure is an exercise left to the reader. 修改此代码以使用固定的警报时间或计算“剩余分钟数”是读者的一项练习。

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

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