1

The below method logouts the user after 15 minutes.But the issue is even if the user is active it will log him out.

I am looking for solution that when user is not active for full 15 min the method it will log him out other than that the method will not run.

public void AutoRedirect()
{
    int int_MilliSecondsTimeOut = (this.Session.Timeout * 900000);
    string str_Script = @"
    <script type='text/javascript'> 
    intervalset = window.setInterval('Redirect()'," + int_MilliSecondsTimeOut.ToString() + @");
    function Redirect()
     {
       alert('Your session has been expired and system redirects to login page now.!\n\n');
       window.location.href='../index.aspx'; 
     }
    </script>";
    UtilityClass.RemoveCookie("login", Response);
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Redirect", str_Script);
}
adiga
  • 34,372
  • 9
  • 61
  • 83
Ayman
  • 75
  • 4
  • 12
  • Each time a page is called check for a session var containing the time a page was last navigated to. If it's less than 15 minutes, update that session variable with the current time and allow the user to continue. Otherwise null the session variable and redirect them to the homepage. – Mike Resoli Sep 11 '17 at 11:04

2 Answers2

2

Instead of doing this with one single timer running 15 minutes, how about setting the interval to 1 minute and in the event count down a counter?

This counter could be easily reset by other events, e.g. AJAX calls, click events etc. which can be easily be triggered in jQuery bv .delegate or .on methods.

If no such resets occur, the count down reaches 0 and you can do the redirect.

var inactivityCountdown = 15;

intervalid = window.setInterval(function Redirect()
 {
   inactivityCountdown--; 
   if (inactivityCountdown<1) {
    clearInterval(intervalid);
    alert('Your session has been expired and system redirects to login page now.!\n\n');
    window.location.href='../index.aspx'; 
   }
 }, 60*1000);

You can see, this way it is easy to reset the counter inactivityCountdown any time before it expires. Don't forget to clear the interval when you do the redirect.

In the following snippet, I am using 10 seconds instead of minutes for better testing. Type something into the text field or click the button to reset the countdown.

You will get the message and then a logout text where you'll be able to login again via a link (this simulates the redirect):

var initCountdownValue = 10;
var inactivityCountdown = initCountdownValue;
$(document).ready(function() {

  $(document).delegate("#login", "click", function(e) {
    location.reload();
  });

  function Logout() {
    document.getElementById('mainpage').style.display = "none";
    document.getElementById('loggedout').style.display = "";
    // or window.location.href="/logout.aspx"; 
  }

  var intervalid = window.setInterval(function Redirect() {
    inactivityCountdown--;
    $("#counter").text(inactivityCountdown.toString());
    if (inactivityCountdown < 1) {
      clearInterval(intervalid);
      Logout();
      alert('Your session has been expired and system redirects to login page now.!');
    }
  }, 1 * 1000);

  $(document).delegate(":input", "click change keydown", function() {
    inactivityCountdown = initCountdownValue;
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div id="mainpage" style="">
  <input type="text"></input>
  <input type="button" text="test"></input>
  <div>
    Counter:
    <div id="counter"></div>
  </div>
</div>
<div id="loggedout" style="display:none">
  You are logged out. To login, click <a id="login" href="#">here</a>
</div>

Of course, in a production environment you would remove the alert and just redirect the user to a logout page. This is just for demo purpose.

Matt
  • 25,467
  • 18
  • 120
  • 187
  • great one more thing how can i show the counter value in asp label thanks – Ayman Sep 11 '17 at 11:51
  • Not sure if I understood what you mean, but I have modified the snippet a bit so it displays the countdown on screen. – Matt Sep 11 '17 at 11:58
0

So the window.setInterval doesn't get overwritten as you may think it is. you could either remove this function and let your IIS or something handle that by configuring it.

or you can just clear the interval and set it again, as said here: How do I stop a window.setInterval in javascript?

Daniel Maiochi
  • 607
  • 6
  • 16