15 May 2009

Implementing Auto-Logout functionality

1 Comment ASP.NET, Web Development

Here is small javascript snippet you will find useful for your web application. It will automatically logout user after specific time of inactivity.

<script type="text/javascript">
var timer;
var wait=10;
document.onkeypress=resetTimer;
document.onmousemove=resetTimer;
function resetTimer()
{
    clearTimeout(timer);
    timer=setTimeout("logout()", 60000*wait);
}
 
function logout()
{
    window.location.href='Logout.aspx';
}    
</script>

Code is self explanatory. We are using setTimeout method which will call a function (logout function in above case) after specified delay in milliseconds. With that, we also resetting that again and again for every user movement on page.

You just need to drop this code in all pages (except login and unsecured pages) of your application. Master page is good place instead for easy maintenance.

Before login off user from application, you may also like to alert user first. So let’s extend above code further. This gonna be bit tricky as we cannot use simple javascript alert to show this message. Instead we will create a DIV layer in the page and use it for showing alert message. Any user interaction will hide this alert message. After specific time of inactivity, user will navigate to Logout.aspx page where you can provide actual implementation to logout user and clear all session objects.

Here is modified code:

<html>
<head runat="server">
<title>Default Page</title>
<script type="text/javascript">
var timer1, timer2;
document.onkeypress=resetTimer;
document.onmousemove=resetTimer;
function resetTimer()
{
    document.getElementById('timeoutPopup').style.display='none';
    clearTimeout(timer1);
    clearTimeout(timer2);
 
    // waiting time in minutes
    var wait=10;
 
    // alert user one minute before
    timer1=setTimeout("alertUser()", (60000*wait)-1);
 
    // logout user
    timer2=setTimeout("logout()", 60000*wait);
}
 
function alertUser()
{
    document.getElementById('timeoutPopup').style.display='block';
}
 
function logout()
{
    window.location.href='Logout.aspx';
}    
</script>
<style type="text/css">
#timeoutPopup {
    display:none; 
    position:absolute; 
    top:0; right:0; bottom:0; left:0;
    width:30%;
    height:30%;
    margin:auto;
    padding:4px;
    border:solid 1px #999999; 
    background-color:#dddddd;
    text-align:center;
}
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="timeoutPopup">
        Your session is about to expire if there is no additional 
        activity.
    </div>
    </form>
</body>
</html>
written by Firoz Ansari
The author didn‘t add any Information to his profile yet.

One Response to “Implementing Auto-Logout functionality”

  1. Rac says:

    hi, is there a way that i can implement it in c#? I tired copying the code inside my project but it doesnt work.is there any way in which you can solve my problem?thanks alot:D

Leave a Reply

Spam protection by WP Captcha-Free