开发者

Confirmation when session timeout in Asp.Net MVC 2

I want to prompt the user when 2minutes will left in session timeout. If user press Yes it refreshes i开发者_开发问答ts 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜