Get the time remaining before the session before the timeout

Is there a way to find out how long it stays on the session object before the timeout expires?

I know how to get the actual timeout from webconfig, but is there a way to find out when the last session of the session?

My users will fill out large forms in my application, and if it takes more than 10 minutes before they click send, they will be logged out. I would like to warn them if the session ends. I will check using ajax call.

+6
source share
3 answers

You can track the time of a DateTime.Now server in a javascript variable.
Compare this to current time in javascript.

If the difference exceeds the threshold, you can display a warning message in javascript.

Even better, you can run an ajax call on your server, and this will extend your server's wait time.

+2
source

You can also set JavaScript timeout to session timeout through some code behind. Get it to run any JavaScript function you want. Very flexible, and you do not need to guess by checking the current time ... etc.

Something like that:

 "window.setTimeout('yourJSFunctionGoesHere()', " + Session.Timeout + ");"; 

If you want to warn 2 minutes before you can do something like:

 "window.setTimeout('yourWarningJSFunctionGoesHere()', " + Session.Timeout-2 + ");"; 
+2
source

We can handle this by specifying the session time and warning time on web.config and using a timer to check the elapsed time on the page.

Web.Config:

 <appSettings> <add key="SessionWarning" value="30" /> </appSettings> <system.web> <sessionState timeout="45"/> </system.web> 

Customer Page:

 <script language="javascript" type="text/javascript"> /* Page Time Out Warning Message Script */ var sessionTimeoutWarning = '<%=System.Configuration.ConfigurationSettings.AppSettings["SessionWarning"].ToString()%>'; var sessionTimeout = "<%= Session.Timeout %>"; var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000; var timeOnPageLoad = new Date(); setTimeout('SessionWarning()', sTimeout); //To redirect to the home page setTimeout('RedirectToWelcomePage()', parseInt(sessionTimeout) * 60 * 1000); //Session Warning function SessionWarning() { //minutes left for expiry var minutesForExpiry = (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning)); var message = "Your session will expire in another " + minutesForExpiry + " mins! Please Save the data before the session expires"; alert(message); var currentTime = new Date(); //time for expiry var timeForExpiry = timeOnPageLoad.setMinutes(timeOnPageLoad.getMinutes() + parseInt(sessionTimeout)); //Current time is greater than the expiry time if (Date.parse(currentTime) > timeForExpiry) { alert("Session expired. You will be redirected to home page"); window.location = "../Home.aspx"; } } //Session timeout add home page where you want to redirect after session timeout function RedirectToWelcomePage() { alert("Session expired. You will be redirected to home page"); window.location = "../Home.aspx"; } /* End */ </script> 
+2
source

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


All Articles