我通过web.config中的以下设置控制我的ASP.NET应用程序中的会话时间。
<configuration>
<system.web>
<sessionState timeout="20" />
</system.web>
</configuration>
我在客户端使用JQuery和JQueryUI的用户会话结束时间前2分钟显示一个警告框
因为,ASP.NET在多个选项卡中共享会话,如果用户在不同选项卡中打开同一页面的多个副本,它们将计为同一会话但在不同时间显示会话超时警报,因为它更多是客户端离开与用户的虚假信息。
例如,如果用户打开第一个标签然后会话开始,他将打开第二个标签@ 10分钟,另一个标签打开18分钟。当他处于第三个选项卡时,第一个选项卡显示会话超时的警报。但第二和第三个标签不会因为它的时间仍然是20分钟。由于用户将在最近的选项卡上,他认为会话在第一个打开的选项卡上过期时仍然处于打开状态。
我不想为每个标签单独开会。
不知道如何处理这个问题,但确定有人可能会有答案。任何输入都会有很大的帮助。
<script type="text/javascript">
var diag = createNewDialog();
var msecPageLoad;
var msecTimeOut;
var msecWarning;
SessionTimerInit();
function SessionTimerInit() {
SetPageTimes();
setTimeout("ShowPendingTimeoutDialog()", msecWarning);
}
function createNewDialog() {
return $('<div></div>');
}
function SetPageTimes() {
msecPageLoad = new Date().getTime();
msecTimeOut = ( <%= Session.Timeout %> * 60 * 1000);
msecWarning = ( <%= Session.Timeout %> * 60 * 1000 * .75); // .75 = 75% of total timeout variable. i.e. if timeout is set for 20 minutes, the dialog should pop up after 15 minutes
//msecWarning = 0;
}
function ShowPendingTimeoutDialog() {
diag.dialog({
autoOpen: false,
title: "Session About to Expire",
position: {
my: "center center",
at: "center center",
of: "#pagecontainer"
},
closeText: "hide",
resizable: false,
draggable: false,
modal: true,
buttons: {
Continue: function() {
ResetTimeout();
}
}
});
UpdateTimeoutMessage();
diag.dialog("open");
}
function ResetTimeout() {
diag.dialog("close");
$.ajax({
url: 'KeepAlive.ashx'
});
SessionTimerInit();
}
function UpdateTimeoutMessage() {
var msecElapsed = (new Date().getTime()) - msecPageLoad;
var timeLeft = msecTimeOut - msecElapsed; //time left in miliseconds
if (timeLeft <= 0) {
RedirectToWelcomePage();
} else {
var minutesLeft = Math.floor(timeLeft / 60000);
var secondsLeft = Math.floor((timeLeft % 60000) / 1000);
var sMinutesLeft = ("00" + (minutesLeft).toString()).slice(-2) + ":";
var sSecondsLeft = ("00" + (secondsLeft).toString()).slice(-2);
diag.html("<p>Your session is about to expire. Please click 'Continue' to continue working.<br /><br />Time Remaining: " + sMinutesLeft + sSecondsLeft + "</p>");
setTimeout("UpdateTimeoutMessage()", 50);
}
}
function RedirectToWelcomePage() {
var diagRedirect = createNewDialog();
diag.dialog("close");
diagRedirect.dialog({
autoOpen: false,
title: "Session Expired",
position: {
my: "center center",
at: "center center",
of: "#pagecontainer"
},
closeText: "hide",
resizable: false,
draggable: false,
modal: true,
buttons: {
OK: function() {
window.location = "../Account/Login.aspx";
diagRedirect.dialog("close");
}
}
});
diagRedirect.html("<p>Your session has expired. Click 'OK' to be redirected to the login page.</p>");
diagRedirect.dialog("open");
}
</script>