c# - Session Available or not -


I am calling a zero function using jquery ajax in mvc3. Even when session comes out in that function, it will also come in Ajax's success function.

Controller action:

  Protected override save zero (query query) I know whether the session is available or not, before sending the request or within the success function of AJAX . , String QueryTitle) {}    

Why not end the session on the server Hold a HTTP 401 unauthorized return, then check this response in jquery and "Your session has expired, please log in again" page

  • <
  • You have the necessary code on the initial server call:

      Save Secure Zero (Query Query, String QueryTitle) { // This bit may be your own Respect string Better Rifactor in sCookieHeader = Request.Headers [ "Cookie"]; If (context sessions! = Null & amp; amp;; Context.Session.IsNewSession & amp; CookieHeader! = Null & amp; cookieHeader.indexOf ("ASP.NETT_SessionId") & gt; = 0) {// session has expired (Request.IsAuthenticated) {FormsAuthentication.SignOut (); } Response.StatusCode = 401} else {// We are authenticated, save therefore}}   

    and on customer:

      $ Ajax (serverUrl, { Data: dataToSave, statusCode: {200: function (feedback) {// All good, continue 401: function (response) {// session expired! // show login box // to authenticate again Call AJAX for / / save call method again},});   

    Your re-authentication call will look something like this:

      Re-authenticate public actions (username, password) {if (IsValidUser (user Name, password)) / / sometimes user roles are used to continue the string userData = string.Join ("|", GetCustomUserRoles ()); FormsAuthenticationTat Ticket = New FormAuthenticationTicket (1, // Ticket Version User Name, // Certified Username DateTime.Now, // Issue Date DateTime.Now UpdateCommand (30), //ExploredidAmerica, // to continue throughout browser sessions Correct user data, / can be used to store additional user data FormsAuthentication.FormsCookiePath); // Path for Cookie / Machine Key String Encrypt a ticket using encrypted ticket = tickets = attestation. Encrypt (ticket); // Add a cookie to the request to save it HttpCookie Cookie = New HttpCookie (FormsAuthentication.FormsCookieName, EncryptedTicket); Cookie HTTP = true; Response.Cookies.Add (cookie); }}   

    (customized from)

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -