2

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?

$(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) {

            }
        );

}
2
  • 1
    Missing some code? What is "promptUserTimeoutId"? Also, why are you setting "timeout" to the function "validationPrompt"? Commented Jun 10, 2011 at 7:43
  • sorry Alejandro, I tried to anonomize my code and made a mess of it - should be clearer now? Commented Jun 10, 2011 at 8:36

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

11 Comments

Hi Darin, thanks for the suggestion - same result though, it doesn't work in my live environment :-(
@james, do you see the AJAX requests being sent? Are they succeeding? Is the timeout of your forms authentication token in web.config higher than 2 * 60 * 1000?
They do get sent, but the response I get back is my login page. So it looks like a fresh request that is not authenticated that then redirects to Login. And yes the timeout is higher than in config.
@james, did you enable slidingExpiration?
@darin - no, it's not set explicitly but I believe defaults to true.
|
1

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

1 Comment

BTW, did you wait that many seconds to see if it is really working? That is a really long time to wait till you find out :)
1

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.

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.