如何以编程方式检测会话过期

时间:2022-10-30 01:19:36

i search google and found many answers for detecting session expiry programmatically. this is code which i saw everyone use it

我搜索谷歌,并通过编程找到了许多检测会话过期的答案。这是我看到每个人都在使用的代码

global.asax
---------------
protected void Session_Start(object src, EventArgs e)
{
if (Context.Session != null && Context.Session.IsNewSession)
{
    string sCookieHeader = Request.Headers["Cookie"];
    if (null != sCookieHeader && sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0)
        Response.Redirect("/Session/Timeout");
}
}

i have few question on the above code 1) when session expire then how Context.Session will not be null? 2) what is the meaning of this line Request.Headers["Cookie"];

我对上面的代码没有什么疑问1)会话过期时上下文如何。会话将不为空?2)这行请求的含义是什么?

looking for good discussion. thanks

寻找良好的讨论。谢谢

2 个解决方案

#1


0  

1) when session expire then how Context.Session will not be null?

1)会话过期时上下文如何。会话将不为空?

Your code triggers when a user comes back to the server with an expired session. The session object in this code is the new empty session which is beginning. Remember you are in Session_Start, not in Session_End.

当用户带着过期的会话返回服务器时,您的代码就会触发。此代码中的会话对象是正在开始的新的空会话。记住你在Session_Start中,而不是Session_End中。

2) what is the meaning of this line Request.Headers["Cookie"];

2)这行请求的含义是什么?

The cookie contains the session's id. If a user is requesting a ressource, providing a session id, and Session_start is triggered, that almost certainly means that the session id refers to an expired session object. A specific message is then displayed to the user.

cookie包含会话的id,如果用户请求一个ressource,提供一个会话id,并且触发Session_start,这几乎肯定意味着会话id是指一个过期的会话对象。然后向用户显示一个特定的消息。

#2


1  

You can use the Session_End method in global.asax file

您可以在全局中使用Session_End方法。asax文件

void Session_End(Object sender, EventArgs E) {
    // Clean up session resources
}

#1


0  

1) when session expire then how Context.Session will not be null?

1)会话过期时上下文如何。会话将不为空?

Your code triggers when a user comes back to the server with an expired session. The session object in this code is the new empty session which is beginning. Remember you are in Session_Start, not in Session_End.

当用户带着过期的会话返回服务器时,您的代码就会触发。此代码中的会话对象是正在开始的新的空会话。记住你在Session_Start中,而不是Session_End中。

2) what is the meaning of this line Request.Headers["Cookie"];

2)这行请求的含义是什么?

The cookie contains the session's id. If a user is requesting a ressource, providing a session id, and Session_start is triggered, that almost certainly means that the session id refers to an expired session object. A specific message is then displayed to the user.

cookie包含会话的id,如果用户请求一个ressource,提供一个会话id,并且触发Session_start,这几乎肯定意味着会话id是指一个过期的会话对象。然后向用户显示一个特定的消息。

#2


1  

You can use the Session_End method in global.asax file

您可以在全局中使用Session_End方法。asax文件

void Session_End(Object sender, EventArgs E) {
    // Clean up session resources
}