views:

186

answers:

2

Hi

I have this problem that my sites uses alot of ajax and when a user times out they still are on my page. Now say a user walks away for 10mins(the timeout is set 10mins) now the user is timed out but still on the page.

So they could do a "save" request but now all my action methods that the ajax requests go to have an asp.net mvc authorize tag ontop of it. If a user fails this authentication then they should be redirected to the "signin page" but since it is ajax request they won't get send to the "signin page" nothing will happen.

So I was thinking maybe I could somehow pass something back(maybe if they are still authenticated) back to ajax.Complete and if it is "false" then do a redirect from javascript.

However I don't know how to do this. I don't know how to pass something back to it, how to write my controller method.

+1  A: 

If MVC produces a 401 Unathorized response then the failure callback for the jQuery ajax call should get invoked and provide you with the XmlHttpRequest object. You can inspect the XmlHttpRequest for as status code of 401 XmlHttpRequest.status == 401 and then change the page location via your script to the login, even provide a message if you like. Regardless if it produces a 401, any non-success status code should invoke the failure callback of the ajax request and you should be able to handle your situation in the method set to handle failures.

Something like the below generalized code could handle your failures.

function(xhr, errorText, ex){
    if(xhr.status == 401) { 
        //.. alert user and redirect to login ..
        document.location = 'myloginpage.html';
    }
    ...
}
Quintin Robinson
Where would I put this code(in which jquery ajax one, success,error,start,stop)? Also there is one case where 200Ok error would come back. See I have Jquery.UI tab and what seems to be happening is that it takes that return Url and loads it up in the actual tab.
chobo2
You would attach it to the error callback. In which case are you getting a 200 ?
Quintin Robinson
Where is the error callback? I probably will get a error message in the following scenario. User walks about for 10mins and timesout and then tries to save something. Save triggers and ajax request to the server. The Authorize tags stops this and probably an error is returned(not sure how to verify which error). The second scenario is this. User walks away for 10mins and timesout this time he hits one of my jquery ui tabs that is ajax enabled. It goes to the server to load up this partial view. But before that the authorize tag stops the users request and send the return url back.
chobo2
But this time the url somehow gets interpreted and loads the actual signin page in that tab they are clicking on.
chobo2
Actually it seems sends a 200 OK in all instances. From what I can tell from firebug when it does the "post" it gives a "302" error and then it does a "get" and that gives a "200" OK.
chobo2
@chobo2: the `error` callback is specified as a parameter to your `$.ajax({...})` call. See some of the examples at http://docs.jquery.com/Ajax/jQuery.ajax to see this in practice. Good luck!
Funka
I have now an error callback sent but no "302" message ever comes through.
chobo2
A: 

An easy way might be to just have the page redirect after the timeout occurs to a logout/login page. Most sites that have timeouts like that will give you a warning first. If you click "OK" your timeout is reset. If you click "Cancel" or just leave it, the page will timeout, you will be logged out and you will be redirected to another page.

Josh Close
Ya but how do I make this warning thing. Like how do I know if the server is about to timeout?
chobo2
What I have done before is, find out when the session will expire on the server, then set a timeout "setTimeout( expression, timeout )" in JS to pop an alert to let the user know that it will timeout soon. If the user clicks OK, make an AJAX call to reset the timeout on the server. If not, there should be a timeout that will redirect the user, that is the set to the same time as the servers session timeout.
Josh Close
Here is an example http://www.sidesofmarch.com/index.php/archive/2005/01/30/friendly-session-timeouts-the-javascript-way/
Josh Close