0

I want to prompt the user when 2minutes will left in session timeout. If user press Yes it refreshes its current session without losing the form data if user select No then it logout its session. I want to know how to capture that two minutes are remaining and how to refresh the current session without losing form data. Im using Asp.Net MVC 2 with JQuery, One more thing Im not using Form Authentication

1
  • What session are you talking about? Is it the HttpSession? Is sliding expiration activated? Also what do you mean by logout its session? Commented Dec 7, 2010 at 10:14

1 Answer 1

1

Well, the session is refreshed after each request. You can find the session timeout on Session.Timeout. You could do something like this in your masterpage:

<script type="text/javascript">
function keepAlive() {
    window.clearTimeout(window.sessionKeepAlive);
    window.sessionKeepAlive = window.setTimeout(function() {

        if(confirm('refresh session?')) {
            // submit ajax request
        } else {
            // logout
        }

    }, <%= (Session.Timeout - 2) * 60 * 1000 %>);
}

keepAlive();
</script>

Then, you need to reset the timeout at every AJAX complete callback, if you're using AJAX:

$(document).ajaxComplete(keepAlive);

It'll be difficult to get this 100% reliable. You need to make sure that the AJAX request in the confirm callback actually touches the state server, and you need to handle what happens when that AJAX request fails. Those are the things that could cause your session to expire even though you have this script.

The other side of the coin is where this will pop up more often than necessary. For instance, if you're adding an image to the DOM through javascript, and the retrieval of that image is touching your state, that request will not reset the javascript timeout, although the session has been refreshed.

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

Comments

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.