Skip to content
Loading
  • Sachin Singh
    +2
    1.   
    2.   
    3. var t;  
    4. window.onload=resetTimer;  
    5. document.onkeypress=resetTimer;  
    6.   
    7. function logout()  
    8. {  
    9.     alert("You are now logged out.")  
    10.     location.href='your login page path'   
    11. }  
    12. function resetTimer()  
    13. {  
    14.     clearTimeout(t);  
    15.     t=setTimeout(logout,1500000) //logs out in 15 min  
    16. }  
    17.   
    18.   
    This will work always.
    +2
  • Srinivasan Ramamoorthi
    +2

    Try this code

    JavaScript
    var idleTime = 0;
    $(document).ready(function () {
        //Increment the idle time counter every minute.
        var idleInterval = setInterval(timerIncrement, 60000); // 1 minute
    
        //Zero the idle timer on mouse movement.
        $(this).mousemove(function (e) {
            idleTime = 0;
        });
        $(this).keypress(function (e) {
            idleTime = 0;
        });
    });
    
    function timerIncrement() {
        idleTime = idleTime + 1;
        if (idleTime > 14) { // 15 minutes
            window.location.reload();
        }
    }
    +2
  • Sachin Singh
    +2
    suppose logout method is
    1. public ActionResult OnPostSend()  
    2. {  
    3.     // logout functionality  
    4.     return new JsonResult(lstString);  
    5. }  
    ajax
    1. $.ajax({  
    2.     type: "POST",  
    3.     url: "/PageName?handler=Send",  
    4.     contentType: "application/json; charset=utf-8",  
    5.     dataType: "json",  
    6.     success: function (response) {  
    7.          
    8.     },  
    9.     failure: function (response) {  
    10.         alert(response);  
    11.     }  
    12. });  
     
    +2
  • Rijwan Ansari
    +1
    You can kill cookies/sessions after 15 minutes as shown
     
    1. app.UseCookieAuthentication(options =>  
    2. {  
    3.    options.ExpireTimeSpan = TimeSpan.FromMinutes(60);  
    4. });  
     
    +1
  • +1
    SetTimeout function can be used for this.  
    setTimeout(logoutUser, 5000);
     function logoutUser()
    {
     window.location = "logout.html";
    }
    +1
  • Vikas Panwar
    +1
    https://stackoverflow.com/questions/46154075/auto-logout-after-15-minutes-of-inactive-c-sharp
    +1