Session timeout handling in a universal HTTP handler

I have an application that uses about 20 http generic handler for ajax call .
I used IReadOnlySessionState to access the session in my handlers. Everything is working fine.

But when the session ends, my handler returns some html, as it redirects to the default page, and the default html of the page is sent back to the response.

To solve this problem.
I checked the session variable in the handler, and if it is null, I wrote

  context.Response.Write("logout") 

And I check jQuery ajax weather, is it a logout or something else.

  $.ajax({ url: "myhandler.ashx", contentType: "application/json; charset=utf-8", success: function (data) { checklogout(data); $("#loading").hide(); }, error: function () { $("#loading").hide(); }, async: false }); 


If this is logout, I used the location to redirect to the login page.
I use form-authentication to authenticate the user.

Is there a better approach for checking and redirecting to the login page using jquery-ajax call.

+6
source share
1 answer

You have handlers in a directory that is automatically controlled by asp.net authentication.

What I have to do is to not automatically manage asp.net authentication by setting it to web.config so that the call to the handler is made live, and the user is not broadcasted, and inside the handlers I will check for this if the user calling this handler, has a session and authentication.

Then in case the user did not have authentication to read this handler, I return a simple flag to my ajax call, then recognize and redirect, for example, like:

 $.ajax({ url: "myhandler.ashx", contentType: "application/json; charset=utf-8", success: function (data) { if(data.redirectToLogin == true) { window.location = "/login.aspx" } else { // do the rest job } }, error: function () { $("#loading").hide(); } }); 
+2
source

All Articles