0

I need help with my MVC2 app session timeout warning. A popup should occur when the user is within "n" minutes of the session timeout. Is there some way to know that information or this has to be calculated. I have standard get/post actions but also AJAX requests. Thanks in advance.

1 Answer 1

1

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:

<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. You could extend the session automatically by making an AJAX request to the server (with the current ASP.NET session cookie, which is important).

Modifying this code to use a fixed alert period or calculating the "minutes left" figure is an exercise left to the reader.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a good start, any may be appropriate in some applications, but it should be noted that it does not support sliding expiration (msdn.microsoft.com/en-us/library/…), updates to the session via AJAX requests, or situations where multiple tabs/windows are open.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.