locked
Detecting and Handling Session TimeOut RRS feed

  • Question

  • User64294366 posted

    I have an application that uses Session and will throw an exception if the Session has timed out. What is the best way to handle session time-outs?

     

    Thank-you,

     

    RD

    Wednesday, January 28, 2009 9:44 AM

Answers

  • User-1171043462 posted

    I have an application that uses Session and will throw an exception if the Session has timed out. What is the best way to handle session time-outs?

     

    Thank-you,

     

    RD

    After login you can add the username to a Session variable

    Session("user") = "UserName"

    On the page load Check

    if Session("UserName") is Nothing Then

    Response.Redirect("Login.aspx")

    End if

    Thus whenever session expires user will be sent back to login page

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 28, 2009 10:01 AM
  • User-1486182340 posted

    Hi,

    read this site. it show's how to redirect to another page with javacript, without user interaction

     

    http://stackoverflow.com/questions/484964/asp-net-push-redirect-on-session-timeout

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 28, 2009 10:04 AM
  • User533502567 posted

    What is the best way to handle session time-outs?

    At page level you can check session on each pageload like this.. 

    if(Session["Session_Name"]==null) 
    
    Response.Redirect("login.aspx"); 
    

     Or

     If you want to redirect the page immediately(with out any user action) after session expires then consider this approach. HTTP is stateless, so you have to refresh the page after session expired. Check this code..  

    Response.AppendHeader("Refresh",
    Convert.ToString((Session.Timeout * 60) + 15) & ";
    URL=Login.aspx")
    

     

    Check this link..

    http://forums.asp.net/p/1373998/2882266.aspx#2882266

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 28, 2009 10:26 AM

All replies

  • User-1171043462 posted

    I have an application that uses Session and will throw an exception if the Session has timed out. What is the best way to handle session time-outs?

     

    Thank-you,

     

    RD

    After login you can add the username to a Session variable

    Session("user") = "UserName"

    On the page load Check

    if Session("UserName") is Nothing Then

    Response.Redirect("Login.aspx")

    End if

    Thus whenever session expires user will be sent back to login page

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 28, 2009 10:01 AM
  • User1464323581 posted

     I've done this in the past in global.asax (Session_End), but there might be other ways, why exactly do you want to throw an exception when ever a session times out? in what context are you trying to do this?

    Wednesday, January 28, 2009 10:02 AM
  • User-1486182340 posted

    Hi,

    read this site. it show's how to redirect to another page with javacript, without user interaction

     

    http://stackoverflow.com/questions/484964/asp-net-push-redirect-on-session-timeout

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 28, 2009 10:04 AM
  • User64294366 posted

    Thanks, That was the simplest solution

    Wednesday, January 28, 2009 10:18 AM
  • User-1171043462 posted

    Thanks, That was the simplest solution

     

    If your query is resolved mark the post that helped you as answer

    Wednesday, January 28, 2009 10:23 AM
  • User533502567 posted

    What is the best way to handle session time-outs?

    At page level you can check session on each pageload like this.. 

    if(Session["Session_Name"]==null) 
    
    Response.Redirect("login.aspx"); 
    

     Or

     If you want to redirect the page immediately(with out any user action) after session expires then consider this approach. HTTP is stateless, so you have to refresh the page after session expired. Check this code..  

    Response.AppendHeader("Refresh",
    Convert.ToString((Session.Timeout * 60) + 15) & ";
    URL=Login.aspx")
    

     

    Check this link..

    http://forums.asp.net/p/1373998/2882266.aspx#2882266

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 28, 2009 10:26 AM