在MVC 3中处理会话超时的方法如下:
var sessionTimeout = 20 * 60 * 1000; // 20 minutes
var timeoutWarning = 18 * 60 * 1000; // 18 minutes
var sessionTimer;
function resetSessionTimer() {
clearTimeout(sessionTimer);
sessionTimer = setTimeout(logout, sessionTimeout);
}
function showTimeoutWarning() {
// Show a warning message to the user
alert("Your session will expire in 2 minutes. Please continue your activity to keep the session active.");
}
function logout() {
// Perform logout action
// Redirect the user to the login page or display a logout message
}
// Start the session timer
resetSessionTimer();
// Bind events to reset the session timer on user activity
$(document).on("mousemove keydown", function() {
resetSessionTimer();
});
// Show the timeout warning when the session is about to expire
setTimeout(showTimeoutWarning, timeoutWarning);
protected void Session_Start()
{
// Set session timeout to 30 minutes
Session.Timeout = 30;
}
<configuration>
<system.web>
<sessionState timeout="30"></sessionState>
</system.web>
</configuration>
领取专属 10元无门槛券
手把手带您无忧上云