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" "">

<html xmlns="">
<head runat="server">
    <title>Show Session Timeout Using JavaScript</title>
    <style type="text/css">
    margin:0px auto;
    <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>

<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";

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


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


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