locked
Web forms redirect when session expires RRS feed

  • Question

  • User-1188570427 posted

    I need to redirect to a session expired page when my session is over.

    I do not have forms authentication, so I can't use that code.

    I tried Server.Transfer(), but it receives a null reference.

    How can I accomplish this?

    Saturday, October 27, 2018 1:26 AM

All replies

  • User465171450 posted

    You can't transfer when the session ends because that happens when the user is no longer connected to the server. If you're setting a session variable for login purposes (never roll your own mechanisms, always use a standard membership/security system). Check for the session variable and if it's null, just Response.Redirect to the session timeout page.

    Saturday, October 27, 2018 2:28 PM
  • User-1188570427 posted

    You can't transfer when the session ends because that happens when the user is no longer connected to the server. If you're setting a session variable for login purposes (never roll your own mechanisms, always use a standard membership/security system). Check for the session variable and if it's null, just Response.Redirect to the session timeout page.

    Well the session has expired and it errors out. 

    So is there a away to redirect without a session? I was thinking server.transfer would work.

    Saturday, November 3, 2018 12:37 AM
  • User409696431 posted

    "So is there a away to redirect without a session? I was thinking server.transfer would work."

    To redirect when?   Server.Transfer behaves differently from Response.Redirect, but both have the requirement that the user's browser be connected to the server to get the result of the transfer, or the instruction to redirect.   They both occur during the processing of a page lifecycle.

    Monday, November 5, 2018 4:30 AM
  • User409696431 posted

    You can't do anything when session expires, but you can check to see if session has expired when the user tries to do something else (once again contacts the server).

    See the following article, for example: http://www.nullskull.com/a/1630/build-an-aspnet-session-timeout-redirect-control.aspx   If you are not using Forms Authentication, just skip those lines.

    Monday, November 5, 2018 11:19 PM
  • User-1324173748 posted

    I tried Server.Transfer(), but it receives a null reference.

    Use below code in your master page code file.

    Response.Redirect("Login.aspx",false)

    Wednesday, November 7, 2018 7:39 AM
  • User-821857111 posted

    Well the session has expired and it errors out. 
    What is it that "errors out"? Are you getting a NullReferenceException because you try to access a Session variable that no longer exists? If so, you should always test for null before you work with session variables. Remember - sessions can be lost unexpectedly for other reasons - not just because they timed out. Never assume that a session variable exists. Always check for null first.

    Wednesday, November 7, 2018 7:52 AM