c# - ASP.NET session redirect without postback -


i have single page application in asp.net c#. informations in application getting loaded using ajax calls. there's no postback @ all. however, every time click on different menu item loads data web services checks if user has valid session. since there's no postback , when session expires users still see main page , when click on menu item data not load since session expired (but users don't know that).

i know how redirect login page when session expires if there's postback, how can achieve same result without postback?

the thing comes mind here use ajax call webservice check if session active, , act accordingly. along these lines (untested):

ashx handler:

<%@ webhandler language="c#" class="checksessionalive" %> using system; using system.linq; using system.web; using system.web.sessionstate; using system.web.script.serialization;  public class checksessionalive : ihttphandler, irequiressessionstate {      public void processrequest (httpcontext cx) {         cx.response.contenttype = "text/json";         cx.response.write(new javascriptserializer().serialize(new         {             result = cx.session["somerandomvalue"] != null ? 1 : -1         }));     }      public bool isreusable {         {             return false;         }     }  } 

javascript (jquery) call:

function checksessionalive() {     $.ajax({         url: "checksessionalive.ashx",         type: "get",         success: function (data) {             if (data.result == 1)                 settimeout(checksessionalive, 5000); // 5 seconds             else                 window.location.href = "http://www.google.com";         }     }); }  $(document).ready(function () {     checksessionalive(); }); 

i hope it's useful :).


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -