Solved

How to redirect to login page automatically, after session time out.

Posted on 2008-05-14
1,361 Views
Last Modified: 2013-11-07
How to redirect to login page automatically, after session time out?
In my web application, user logs in in the application, after a certain time (30 mins) session times out and login page appears only AFTER user action.
I would like to redirect the user back to the login screen automatically (before user performs any action) after session has timed out.

Your suggestions will be greatly appreciated.
I used following code in pagerender of masterpage.

 

 HttpContext.Current.Response.AppendHeader("Refresh", Convert.ToString(((HttpContext.Current.Session.Timeout * 60) - 5)) + "; Url=homepage.aspx")

 

Also, i have Session_Start/Session_end in global.asax.

Open in new window

0
Question by:sivachirravuri
    9 Comments
     
    LVL 21

    Expert Comment

    by:naspinski
    There is no 'good' way to do this.  

    One would be to simply include a META refresh tag set to the time you want to refresh (the 3600 is 3600 seconds/60 minutes):

    <meta http-equiv="refresh" content="3600;url=http://yoursite/login.aspx"/>

    Another way would be to call a Javascript or C#/VB timer and call the refresh when the timer hits a certain time.  With the C# option, you would check to make sure the session is dead.

    If you included one of these methods on your page, it would be self-regulating since it would refresh every time the page refreshes which would correspond to the session refreshing.  
    0
     
    LVL 5

    Author Comment

    by:sivachirravuri
    Thanks for you suggessions.
    I have created a Javascript timer in class file & refreshing page. But i m getting JS error at the time of page refresh.
    Can you suggest this appraoch with some sample code.
    0
     
    LVL 21

    Expert Comment

    by:naspinski
    Keeping it as simple as possible:

    <meta http-equiv="refresh" content="3600;url=http://yoursite/login.aspx"/>
    0
     
    LVL 5

    Author Comment

    by:sivachirravuri
    Problem for using <meta http-equiv="refresh" content="3600;url=http://yoursite/login.aspx"/> is
    I have almost 70-100 pages in my website. It will be hectic for me to include this in all pages.

    Thanks, Sp
    0
     
    LVL 21

    Expert Comment

    by:naspinski
    understandable, you could use this if you were using masterpages, are you?

    If not I can throw together a js example.
    0
     
    LVL 21

    Accepted Solution

    by:
    Javascript:
    function timeout(){window.location = "http://google.com"}

    call it in your html:
    <body onload="setTimeout('timeout()', 5000)">

    But you would still have to put this into every page... not sure how you are going to accomplish this without masterpages.
    0
     
    LVL 5

    Author Comment

    by:sivachirravuri
    Yes, I use masterpages. But, can we give the following line master pages? I am not aware of that.
    <meta http-equiv="refresh" content="3600;url=http://yoursite/login.aspx"/>
    If yes, then my problem will be solved.

    Alternative option:
    This option I am trying to implement. I have global Pagebase.vb file that is called in all the forms. I will implement in that.
    Javascript:
    function timeout(){window.location = "http://google.com"}
    call it in your html:
    <body onload="setTimeout('timeout()', 5000)">

    Regards,
    0
     
    LVL 21

    Expert Comment

    by:naspinski
    If you place that in the <head> of your masterpage, it will be replicated on each page that uses the master.
    0
     
    LVL 5

    Author Closing Comment

    by:sivachirravuri
    Thanks for the quick replies. It solved my problem. Thanks a Ton.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

    680 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    31 Experts available now in Live!

    Get 1:1 Help Now