开发者

Using Javascript setTimeout to call an MVC action to keep alive forms authentication

I am using setTimeout to call an MVC action to try and keep alive the users forms authentication.

When I call this code normally (ie not using setTimeout) it DOES keep the forms authentication alive.

When called within setTimeout it does not.

My question is why does this not work when called via setTimeout?

开发者_如何学Python
$(document).ready(function() {

    DoKeepAlive();    //simple test when page loads....does keep forms auth alive
});


var timeout= 0;
timeout= setTimeout(validationPrompt, 2 * 60 * 1000);  

function validationPrompt() {

    var answer = confirm("Your session will timeout in less than a minute. Click OK to stay logged in, and reset the timout period.")
    if (answer) {

        DoKeepAlive();     //when called in here by setTimeout, does NOT keep forms auth alive

        //re-set the 10 minutes count
        clearTimeout(timeout);
        timeout= setTimeout(validationPrompt, 2 * 60 * 1000); 
    }
    else {
        var URL = "<%= Url.Content("~/Account/Logoff") %>"
        window.location = URL;
    }
}

function DoKeepAlive(){

        var URL = "<%= Url.Content("~/Account/ValidateAuthentication") %>"
    $.post(
            //"../../Account/ValidateAuthentication",      
            URL,
            function(data) {

            }
        );

}


It's difficult to say why your code is not working. I would try to simplify it first:

<script type="text/javascript">
    window.setInterval(function () {
        var answer = confirm("Your session will timeout in less than a minute. Click OK to stay logged in, and reset the timout period.");
        if (answer) {
            $.post('<%= Url.Action("ValidateAuthentication", "Account") %>');
        } else {
            window.location.href = '<%= Url.Action("Logoff", "Account") %>';
        }
    }, 2 * 60 * 1000);
</script>


try putting timeout= setTimeout(validationPrompt, 2 * 60 * 1000); inside $(document).ready ?


After much frothing back and forth, I discovered today the time on the server I was using was 3 minutes slow. A post on this thread triggered helped me.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜