简体   繁体   中英

ASP.NET 'InProc' timeout - execute javascript code at the end

In my ASP.NET project, I am using 'inproc' session-state for session management and current the expiry is set as -

      <sessionState mode="InProc" timeout="1" ></sessionState>

ie the session time-out is 1 minute.

At the end of an 'idle' 1 minute, the application is redirected to the session-expired view or page.

Before this redirect happens, I want to execute some javascript code in the same controller context. Another point here is that the session-expiry code is called from other places apart from framework's inbuilt mechanism.

I've tried creating a javascript 'poll' function to check for session timeout and then execute something on session invalidation but what this is doing is in-turn re-validating the session every time the xhr call is made.

The basic idea is for the javascript code to be informed of the session timeout through some mechanism. The code base is an existing one and pretty large for a new .NET developer like me to understand.

Any help is appreciated.

I have used the following approach to warn the users 2 minutes before the session timeout of 20 minutes so that they can save their work and continue within 2 minutes otherwise their changes will be lost

<script type = "text/javascript">
var sessionTimeoutWarning = 18;
var sessionTimeout = '<%= Session.Timeout %>';
var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;
setTimeout('SessionWarning()', sTimeout);

function SessionWarning() {
  var message = "Your current session will expire in " + (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning)) + " mins!\nPlease Save any unsaved changes before the session expires";
  var userid = '<%= Session("User_ID") %>';
  if (userid == '') {} else {
    if (parseInt(userid) > 0) {
      alert(message);
    }
  }
} 
</script>

Its an ASP.NET web forms application, the above script block has been inserted on a master page and available in all the sub pages.

Session("User_ID") is a code behind variable that stores currently logged in User's ID which must be positive non-zero integer. The logic is, if it is empty, zero or non-positive, do not display the message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM