如何将用户重定向到特定页面作为会话过期或放弃

时间:2022-07-09 01:16:58

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 automatically as specific session expire? so that I write logic there. I search about global.asax then I use this but it is not working..

我只是用会话对象创建一个登录页面。在会话中存储一些值(userId,userName,UserRoleId)。据我所知,会话的默认时间是20分钟。我希望如果用户在观看页面并且会话到期时将其重定向到登录页面。如果可能存在任何事件,当特定会话到期时会自动触发该事件?所以我在那里写逻辑。我搜索关于global.asax然后我使用它但它不工作..

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

3 个解决方案

#1


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.

我没有测试过,但逻辑应该没问题。

#2


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.

您可能必须使用javascript在客户端执行此操作。您可以调用将检查会话状态的操作,然后通过为页面设置新位置来重定向用户。

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.

如果要从服务器驱动它,可以使用Signalr和集线器。当会话到期时,集线器调用客户端(仍然在js中),您将执行重定向。

#3


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.

这将页面重定向为20分钟加上5秒到login.aspx。

#1


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.

我没有测试过,但逻辑应该没问题。

#2


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.

您可能必须使用javascript在客户端执行此操作。您可以调用将检查会话状态的操作,然后通过为页面设置新位置来重定向用户。

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.

如果要从服务器驱动它,可以使用Signalr和集线器。当会话到期时,集线器调用客户端(仍然在js中),您将执行重定向。

#3


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.

这将页面重定向为20分钟加上5秒到login.aspx。