问题描述
为什么 ASP.NET 页面中 Session 对象上的 SessionID 属性在请求之间会发生变化?
我有一个这样的页面:
...<div>会话 ID:
为什么 ASP.NET 页面中 Session 对象上的 SessionID 属性在请求之间会发生变化?
我有一个这样的页面:
...<div>会话 ID:
...
每次我按 F5 时,输出都会不断变化,与浏览器无关.
这就是原因
当使用基于 cookie 的会话状态时,ASP.NET 不会为会话数据分配存储,直到使用 Session 对象.因此,在访问会话对象之前,会为每个页面请求生成一个新的会话 ID.如果您的应用程序需要整个会话的静态会话 ID,您可以在应用程序的 Global.asax 文件中实现 Session_Start 方法并将数据存储在 Session 对象中以修复会话 ID,或者您可以在您的其他部分使用代码应用程序在 Session 对象中显式存储数据.
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.sessionid.aspx
所以基本上,除非你在后端访问你的 session 对象,否则每个请求都会生成一个新的 sessionId
编辑
此代码必须添加到文件 Global.asax 中.它会向 Session 对象添加一个条目,以便您修复会话直到它过期.
protected void Session_Start(Object sender, EventArgs e){会话[初始化"] = 0;}
Why does the property SessionID on the Session-object in an ASP.NET-page change between requests?
I have a page like this:
...
<div>
SessionID: <%= SessionID %>
</div>
...
And the output keeps changing every time I hit F5, independent of browser.
This is the reason
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.sessionid.aspx
So basically, unless you access your session object on the backend, a new sessionId will be generated with each request
EDIT
This code must be added on the file Global.asax. It adds an entry to the Session object so you fix the session until it expires.
protected void Session_Start(Object sender, EventArgs e)
{
Session["init"] = 0;
}
这篇关于ASP.NET:请求之间的 Session.SessionID 更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!