ASP.NET:请求之间的 Session.SessionID 更改 [英] ASP.NET: Session.SessionID changes between requests

查看:42
本文介绍了ASP.NET:请求之间的 Session.SessionID 更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 ASP.NET 页面中 Session 对象上的 SessionID 属性在请求之间会发生变化?

我有一个这样的页面:

<代码>...<div>会话 ID:<%= 会话 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

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.

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 更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆