开发者

Detect Session Timeouts / Distinguish Between First Visit & Session Timeout

开发者 https://www.devze.com 2023-01-11 03:02 出处:网络
When a user goes to the sign in page, I want to detect if their session timed out and was redirected to this page, so a friendly message could be displayed.

When a user goes to the sign in page, I want to detect if their session timed out and was redirected to this page, so a friendly message could be displayed.

I set isTimeout = true when the session is a new session and when the cookie["ASP.NET_SessionId"] is开发者_如何转开发 not null. But isTimeout was set to true when if it was a first visit too. How do I distinguish the first visits from timeouts?

Thanks in advance!


In your Global.asax there is a method called Session_End to handle just this.

You can use this to add whatever functionality you need. Such as setting TempData["IsTimeout"] to true (if you are using ASP.NET MVC). This will then persist past the redirect and is accessible on your log in view. It will then be destroyed.

E.g. In your Global.asax.cs

protected void Session_End(Object sender, EventArgs e) 
{ 
    TempData["IsTimeout"] = true;
} 

In your log in view:

<%: ((bool)(TempData["IsTimeout"] ?? false)) ? "For security reasons you were timed out, please log in again" : "" %>
0

精彩评论

暂无评论...
验证码 换一张
取 消