How to reset ASP.Net sessions determine state timeout using ajax request

I am working on an ASP.Net Full-Ajax website. Since all operations are performed on Ajax requests, any postback request will not be sent to the server. In the other hand, I have a javascript timer that sends an ajax request to check sessions and, if it has expired, redirects the user to the login page.

The problem here is: when the user works on the page for 20 minutes, and all operations will be performed using ajax requests in 20 minutes, the session has expired, and the user will be redirected to the login page (with this javascript timer) exactly while working with the page. Therefore, I need to update the session state using an ajax request. But how? How can I do this with a reset state timeout session using an ajax request!? !!

Depending on the results of Google search, I can’t be stub using an ajax request, because the SessionId store is on the client as a cookie and to update it you must first request a response for feedback: (

Note : session state is set in InProc mode using timeout = 20

Sorry for my poor English syntax, I'm new to English

Regards, Foroughi

UPDATE : does the ajax request update session check the status? !!

UPDATE When my user visited the site, I scheduled the session as follows:

 Session["UserId"] = UserObject.Id; 

and throughout my page I use some kind of web method to work with the stub as follows:

 [WebMethod] public static Opr1 (Paramethers...) { //Question is here , how can i update UserId session to prevent to expire,how can i update it //execute my codes to preform Opr1 } 
+4
source share
2 answers

If you use WebMethods, you should decorate your methods, for example

 [WebMethod(EnableSession = true)] 

In addition, if you need to keep your session alive, you should try to create an HTTPHandler that implements IRequireSessionState, this interface allows you to receive / set (deserialize / serialize) session variables that will ultimately shift the session timeout.

This is a good article about ajax sessions and calls.

http://seejoelprogram.wordpress.com/2008/11/10/maintaining-aspnet-session-state-in-an-ajax-application/

I hope it will be useful

Sincerely.

+3
source

I do like this:

I have a simple web service to check if the user is verified, but you can change the code to check if the session has ended.

 <%@ WebService Language="C#" Class="CheckAutheticated" %> using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class CheckAutheticated : System.Web.Services.WebService { [WebMethod] public string checkAuthenticated() { return "authenticated"; } 

}

then I call the client side:

 function checkAuthenticated() { { $.ajax({ type: "POST", url: "CheckAutheticated.asmx/checkAuthenticated", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: checkAuthenticatedOk, error: checkAuthenticatedError }); } } function checkAuthenticatedOk() { } function checkAuthenticatedError() { $("#sessionExpiredCover").show(); } 

here are some basic css

 <style type="text/css"> #sessionExpiredCover { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 100000; display: none; background-color: #fff; /*opacity: .7; filter:progid:DXImageTransform.Microsoft.BasicImage(opacity=.7);*/ } </style> 

and finally div

  <div id="sessionExpiredCover"> <div style="background-color:#fff; margin:100px; height:200px; width:400px;"><h1>Session expired</h1> <br /> <asp:HyperLink NavigateUrl="~/Login.aspx" runat="server" Text="Please log in again" /> </div> </div> 
0
source

Source: https://habr.com/ru/post/1397568/


All Articles