【问题标题】:Session timeout in Web appicaltionWeb 应用程序中的会话超时
【发布时间】:2019-06-23 14:44:09
【问题描述】:
在一个 aspx 网络表单应用程序中。
我设置了session.timeout 10 分钟。
但网页每 5 分钟发送一次自动 ajax 请求(使用 javascript 计时器)
因此即使用户闲置超过 10 分钟也不会发生会话超时。
如果用户保持空闲状态,用户会话应该在 10 分钟后过期,不要考虑自动 ajax 请求。有没有办法检查上次手动用户交互时间?
我们将不胜感激任何解决方案
【问题讨论】:
标签:
c#
asp.net
ajax
session
iis
【解决方案1】:
这是一个使用 JQuery 处理 mousemove 和 keypress 事件的简单脚本。如果时间到期,页面会重新加载。
客户端:
<script type="text/javascript">
var idleTime = 0;
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval(timerIncrement, 60000); // 1 minute
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
});
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 9) { // 10 minutes
// Here You'd have to have a little ajax call to server to set all session variable value as null.
}
}
function setAllSessionNull(){
//make ajax call to server to set all session variable value as null.
// redirect to login page
}
</script>
获取所有会话并设置为空的 c# 代码:
for (int i = 0; i < Session.Count; i++)
{
Session.Remove(Session.Keys[i]);
}