SessionID 在每次寻呼时改变

SessionID changing at every page call

我正在 class 中使用此代码访问 SessionID:

HttpContext.Current.Session.SessionID;

但是我发现 SessionID 在每个页面回发时都会发生变化,这种情况会在很短的时间内发生,因此当前会话不应已经过期。我认为 SessionID 在整个时间内保持不变,直到过期。

您应该在应用程序 Global.asax 文件中使用 Session_Start 方法。下面Link可能对你有帮助

ASP.NET: Session.SessionID changes between requests

When using cookie-based session state, ASP.NET does not allocate storage for session data until the Session object is used. As a result, a new session ID is generated for each page request until the session object is accessed. If your application requires a static session ID for the entire session, you can either implement the Session_Start method in the application's Global.asax file and store data in the Session object to fix the session ID, or you can use code in another part of your application to explicitly store data in the Session object.

喜欢

protected void Session_Start(Object sender, EventArgs e) 
{
    Session["init"] = 0;
}