Monday, February 13, 2012

Show session timeout and redirect to login page using javajcript



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Show Session Timeout Using JavaScript</title>
    <style type="text/css">
    p{
    margin-top:40px;
    font-size:25px;
    margin:0px auto;
    width:auto;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <h1>Show session timeout and redirect to login page using javajcript.please do't refresh the page.</h1>
    <p>Keep patience and wait for 1 minutes you will get session expire warning message.</p>
    <p>After warning message if you do not respond to it you will be redirected to login page in next 2 minutes.</p>
    </form>
</body>
</html>

<script language="javascript" type="text/javascript">
     
       //Session timeout in minute.
       var sessionTimeout = "<%= Session.Timeout %>";

       //Session timeout warning before 2 minute.
       var sessionTimeoutWarning = parseInt(sessionTimeout)-2;

       //Session timeout in millisecond.
       var sTimeout = parseInt(sessionTimeout) * 60 * 1000;

       setTimeout('SessionWarning()', sTimeout);

       function SessionWarning() {
           //Calculating minutes before timeout in millisecond.
           var minutesForExpiry = (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning));

           var message = "Your session will expire in another " + minutesForExpiry + " mins! Please refresh the page before the session expires";
           alert(message);

           setTimeout('Redirect()', (minutesForExpiry * 1000 * 60));

       }

       function Redirect() {
           alert("Session expired. You will be redirected to login page");
           window.location = "Login.aspx";
       }
</script>


*******************************************************************************


using System;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Timeout = 3;//in minute
    }
}



Post a Comment