Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET Session fails to renew itself
    primarykey
    data
    text
    <p>I've been tasked with adding functionality to an ASP.NET 4.0 Web Forms application to warn the user shortly before their session ends, and to provide the option either to continue the session, or to end the session. </p> <p>I have implemented this via a confirmation dialog box which warns the user that the session is going to end soon and giving the option of pressing 'OK' to continue the session, or pressing 'Cancel' to end the session. </p> <p>On pressing 'cancel', the page redirects to the logout page. </p> <p>On pressing 'OK', I invoke a JQuery GET request on an empty ASPX page in my application (KeepAlive.aspx). As I understand, when a user makes a request to the page, ASP.NETshould take care of renewing the session - and thus resetting the session timeout. </p> <p>However, I found that when the user presses OK, the session is not extended, and so it times out. This is despite the GET request apparently being successful (e.g. the callback function is invoked). </p> <p>The code I used to implement this exists as a JavaScript function which gets invoked via the <code>onload</code> event on the master page - so it's inherited by all the other pages in the application. </p> <pre><code>var intervalID; /* Set a timeout interval based on the server timeout value (-10%) */ function setTimeoutInterval() { /* Session timeout warning dialog */ // Get session timeout value var timeoutMins = "&lt;asp:ContentPlaceHolder id='timeoutPlaceholder' runat='server'&gt;&lt;%= Session.Timeout %&gt;&lt;/asp:ContentPlaceHolder&gt;"; // Subtract 10% of the timeout value - to give the user a chance to continue the session before it expires var remainingTimeMins = Math.ceil(timeoutMins * 0.1); var timeoutMins = timeoutMins * 0.9; // Convert the timeout value to milliseconds var timeout = timeoutMins * 60 * 1000; // Set javascript timeout intervalID = window.setInterval("displayTimeoutDialog(" + remainingTimeMins + ")", timeout); } /* Display a dialog prompting the user to continue the current session or to end the session */ function displayTimeoutDialog(remainingTimeMins) { var result = confirm("The session will end in ~" + remainingTimeMins + " minute(s). Press OK to continue, or Cancel to log out."); if (result == true) { // Keep the session alive alert("Keep alive!"); $.get("KeepAlive.aspx", function() { alert("Successful request"); }); } else { // Redirect to the logout page window.location.href("Logout.aspx"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload