我的任务是向ASP.NET 4.0 Web窗体应用程序添加功能,以便在会话结束前不久警告用户,并提供继续会话或结束会话的选项。
我已经通过确认对话框实现了这一点,该对话框警告用户会话将很快结束,并且可以选择按“确定”继续会话,或者按“取消”结束会话。
按“取消”后,页面将重定向到注销页面。
按'确定',我在我的应用程序(KeepAlive.aspx)中的空ASPX页面上调用JQuery GET请求。据我所知,当用户向页面发出请求时,ASP.NET应该负责更新会话 - 从而重置会话超时。
但是,我发现当用户按下OK时,会话不会被扩展,因此会超时。尽管GET请求显然是成功的(例如,调用了回调函数)。
我用来实现它的代码作为JavaScript函数存在,它通过母版页上的onload
事件调用 - 因此它被应用程序中的所有其他页面继承。
var intervalID;
/* Set a timeout interval based on the server timeout value (-10%) */
function setTimeoutInterval()
{
/* Session timeout warning dialog */
// Get session timeout value
var timeoutMins = "<asp:ContentPlaceHolder id='timeoutPlaceholder' runat='server'><%= Session.Timeout %></asp:ContentPlaceHolder>";
// Subtract 10% of the timeout value - to give the user a chance to continue the session before it expires
var remainingTimeMins = Math.ceil(timeoutMins * 0.1);
var timeoutMins = timeoutMins * 0.9;
// Convert the timeout value to milliseconds
var timeout = timeoutMins * 60 * 1000;
// Set javascript timeout
intervalID = window.setInterval("displayTimeoutDialog(" + remainingTimeMins + ")", timeout);
}
/* Display a dialog prompting the user to continue the current session or to end the session */
function displayTimeoutDialog(remainingTimeMins)
{
var result = confirm("The session will end in ~" + remainingTimeMins +
" minute(s). Press OK to continue, or Cancel to log out.");
if (result == true) {
// Keep the session alive
alert("Keep alive!");
$.get("KeepAlive.aspx", function() { alert("Successful request"); });
}
else {
// Redirect to the logout page
window.location.href("Logout.aspx");
}
}
答案 0 :(得分:1)
我想知道AJAX GET是不够的。可能是GET不会触发整个页面生命周期,因此会话不会更新。您是否尝试重新加载页面而不是仅发出GET?我知道它不那么优雅,但知道会很有趣。
答案 1 :(得分:1)
我们找到了一个不需要刷新用户所在页面的解决方案。
这是通过页面上隐藏的HTML iframe
实现的:
<iframe id="keepAlive" src="KeepAlive.aspx" frameborder="0" width="0" height="0" runat="server"></iframe>
我将实现计时器和对话框的javascript方法添加到iframe页面。当用户想要续订会话时,他们按对话框按钮上的“继续”,然后我们只需刷新iframe页面。刷新会导致服务器更新会话,而不会让用户在他们正在处理的页面上丢失 - 因为它是刷新的iframe,而不是用户所在的实际页面。
请记住,所有这些代码都位于ASP.NET母版页中,因此它会在我们的应用程序中的每个ASPX页面上继承。 iFrame上的Javascript在正文onload
事件中调用。
var intervalID;
/* Set a timeout interval based on the server timeout value (-10%) */
function setTimeoutInterval()
{
/* Session timeout warning dialog */
// Get session timeout value
var timeoutMins = "<%= Session.Timeout %>";
// Subtract 10% of the timeout value - to give the user a chance to continue the session before it expires
var remainingTimeMins = Math.ceil(timeoutMins * 0.1);
var timeoutMins = timeoutMins * 0.9;
// Convert the timeout value to milliseconds
var timeout = timeoutMins * 60 * 1000;
// Set javascript timeout
intervalID = window.setInterval("displayTimeoutDialog(" + remainingTimeMins + ")", timeout);
}
/* Display a dialog prompting the user to continue the current session or to end the session */
function displayTimeoutDialog(remainingTimeMins)
{
var result = confirm("The session will end in ~" + remainingTimeMins +
" minute(s). Press OK to renew, or Cancel to let your session expire.");
if (result == true) {
// Keep the session alive and clear the interval
alert("Session renewed.");
window.location.href("KeepAlive.aspx");
}
else {
// Redirect to the logout page
window.clearInterval(intervalID);
}
}
答案 2 :(得分:0)
确保您的请求包含相同的会话ID,并且您没有使用Cookieless SessionID。
请参阅http://msdn.microsoft.com/en-us/library/ms178581%28v=vs.90%29.aspx
会话标识符
会话由可以读取的唯一标识符标识 使用SessionID属性。为会话状态启用时 ASP.NET应用程序,每个请求应用程序中的页面 检查从浏览器发送的SessionID值。如果没有SessionID 提供了值,ASP.NET启动一个新会话和SessionID 该会话的值将通过响应发送到浏览器。
默认情况下,SessionID值存储在cookie中。但是,你可以 还配置应用程序以在URL中存储SessionID值 用于“无cookie”会话。
只要继续请求,会话就被视为活动 具有相同的SessionID值。如果请求之间的时间 特定会话超过指定的超时值(以分钟为单位), 该会话被视为已过期。请求已过期 SessionID值导致新会话。