简体   繁体   中英

How to redirect user to specific page as Session Expire or abandon

I just make a Login page with session object. Storing some values in session (userId, userName, UserRoleId). As I know that default time of session is 20 mins. I want that if user watching a page and as session expiry happen then redirect him to login page . How it possible is there any event exists which fire as specific session expire? 触发? so that I write logic there. I search about global.asax then I use this but it is not working..

void Session_End(object sender, EventArgs e) 
    {
        Response.Redirect("login.aspx");
    }

On your master page class :

Stopwatch stopWatch = new Stopwatch();

On your master page page-load:

public Page_Load()
{
if (!page.ispostback)
{

   System.Threading.Timer TimerForSessionExpire = new System.Threading.Timer(TickForSessionExpire, null, 0, 6000*60); // check after every 1 minute
}
else
{
   stopWatch.reset();
   stopWatch.start();
}
}

public void TickForSessionExpire()
{
   if (stopWatch.Elapsed.TotalMinutes>20)
   {
       Response.Redirect("login.aspx");
   }
}

I have not tested this but logic should be fine.

You'll probably have to do it at the client side using javascript. You can call an action which will check the status of your session and then redirect the user by setting a new location for your page.

If you want to drive it from the server, you can use Signalr and a hub. When the session expires, the hub call the client (still in js) and you will perform the redirection.

You could add <meta http-equiv="refresh" content="1205; url=http://domain/login.aspx"> to all your responses.

This redirect the page at 20 min plus 5 seconds to login.aspx.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM