Detecting authentication timeout in the handler and redirecting the entire page to the login page

I am writing an application where someone can edit paragraphs on a web page. jQuery is used to send and receive edited paragraph data to a handler that saves it or reads from the database. The problem is that if forms authentication has ended, I get the login page from my handler. Is there a way I can detect on the client or server if the authentication timeout and the whole page are redirected to the login page?

+4
source share
2 answers

In my client side application, I check the HTTP status of the returned page. If it is 401 (Unauthorized error), I submit a form for entering the modal dialog. You can re-authenticate or simply redirect using

window.location = 'http://someurl.com'; 

This requires server interaction with a 401 return, but it seems to me in the cleanest way.

+1
source

I recently developed a 401 processing method that I think will work for you too. The problem with previous versions of .NET is that error pages do not return the correct error code, but only 302 to the specified page.

With .NET 3.5, your error handler can overwrite instead of redirecting with redirectMode:

 <customErrors mode="On" defaultRedirect="/err.aspx" redirectMode="ResponseRewrite"> <error statusCode="404" redirect="/404.aspx"/> </customErrors> 

Since all requests (including web service calls) now return the correct HTTP status code, you can correctly use the 404/401/500 code in your javascript.

Then in your client code (this is in jQuery and redirects to any errors not only 401s, but you get the idea):

 $(document).ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError) { window.location.href('/error.aspx'); }); 
+1
source

All Articles