Troy Mitchel
Troy Mitchel

Reputation: 1810

Redirect Page when session timeout expires

I want to re-direct the user to Home page when the session has expired during a post back. When the form timeout occurs then user is directed to login page.

Session timeout = 20, Form timeout = 2880

I tried the following code in the Global.asax, but does not work and causes Application_Error ("Response is not available in this context.").

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when the session ends
    Response.Redirect("~/HomePage.aspx")
End Sub

Upvotes: 3

Views: 10154

Answers (3)

David East
David East

Reputation: 32624

It's a property in your web.config

<forms name="WhateverYourAuthNameIs" 
       loginUrl="~/HomePage.aspx" 
       defaultUrl="~/Default.aspx" 
       protection="All" path="/" 
       cookieless="AutoDetect"/>

When the expiration occurs they will be sent to the page that loginUrl is set to.

Upvotes: 1

freefaller
freefaller

Reputation: 19963

The Session_End is not initiated by the browser (because any response from the browser should in theory extend the Session by another 20 minutes), therefore the concept of a Response object doesn't make sense.

The only option I can see would be to have some sort of client side script (javascript) that utilises the window.setTimeout function... something like (untested)...

<body onload="window.setTimeout(function(){document.location.href='/Home.aspx';},72000000);">

UPDATE

Having read the request again, the requirement is slightly ambiguous. If the requirement is for the webpage to automatically redirect after the 20 minutes (without user intervention), then my answer addresses that.

However, I think it's more likely that the requirement is to redirect to the homepage if the user sends another request to the server after the 20 minutes. In which case, MajoB's answer is a good one.

Upvotes: 2

Marian Ban
Marian Ban

Reputation: 8188

If you cant use Server.Transfer or Respose.Redirect then you can check for some session variable in page_load event of each page. And if is this session variable is null then redirect to specific page. You can create base page to avoid code duplication.

Upvotes: 0

Related Questions