c# - Session Available or not -
I am calling a Controller action: 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: and on customer: Your re-authentication call will look something like this: (customized from) zero function using jquery ajax in mvc3. Even when
session comes out in that function, it will also come in Ajax's success function.
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) {}
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}}
$ 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},});
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); }}
Comments
Post a Comment