0

I made a simple web app using asp.net C# and I made a code to logout automatically from the website and redirect the user to the login page when the session expire, but I have a problem which is the website logout and redirect the user to the login page even if the user is active and clicking on buttons and moving the mouse and in this case I want the session to be alive and make it expire when the user don't do anything during the timeout period just like most of the websites.

Here is my code to end session:

web.config:

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

main_page.aspx:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    AutoRedirect();
}

public void AutoRedirect()
{
    int int_MilliSecondsTimeOut = (this.Session.Timeout * 6000);
    string str_Script = @"
           <script type='text/javascript'> 
               intervalset = window.setInterval('Redirect()'," +
                   int_MilliSecondsTimeOut.ToString() + @");
               function Redirect()
               {
                   window.location.href='/login.aspx'; 
               }
           </script>";

    ClientScript.RegisterClientScriptBlock(this.GetType(), "Redirect", str_Script);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jim
  • 17
  • 6

1 Answers1

0

let not talk about Application's timeout How to set session timeout in web.config

In your Javascript I think you need to stop setInterval Stop setInterval call in JavaScript

so why not use all task in aspx page 's javascript like

$(document).ready(function(){
     ResetTheTimer();
     $('body').mousemove(function() { // or other events
           ResetTheTimer();
           alert('clear!');
     });
 
});
var intervalset ;
var MilliSecondsTimeOut = 12000;
function ResetTheTimer(){
    
    clearInterval(intervalset);
    intervalset = window.setInterval(Redirect,  MilliSecondsTimeOut );
             
}
  function Redirect()
  {alert(1);   
  //window.location.href='/login.aspx'; 
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

And then to make application alive you need some interval to postback to code behind too.

user3682728
  • 467
  • 3
  • 7
  • thanks for the replay I tried your answer but it doesn't logout and finish the session – jim Dec 22 '21 at 11:47
  • It doesn't logout and finish the session , you mean it's not go to login.aspx or it's go to the page and not terminate user's session . Could you verify that the JavaScript is working by remove $('body').mousemove part ? – user3682728 Dec 23 '21 at 06:46
  • the session stay alive and ever end even that I set MilliSecondsTimeOut = 600 and left the mouse but still the session alive but it should end if I am not moving the mouse – jim Dec 23 '21 at 08:32
  • also there is a little mistake in the first line in the code it should be like this: $(document).ready(function () { – jim Dec 23 '21 at 08:33
  • Sorry,there are some mistake in my code . I updated my script please try/edit it from code snippet , the alert 1 mean the redirect is called – user3682728 Dec 23 '21 at 08:55
  • @jim but can you show the MilliSecondsTimeOut value dynamically decreasing I tried display it but it show only the default first value 12000 – jim Dec 23 '21 at 12:16