ASP.NET 网站 HttpContext.Items 在 HTTPHANDLER 中随机为空
有时,当调用 Http 处理程序时,我无法访问 HttpContext.Items[typeof(UserProfile)] 对象,其中 UserProfile 是通过表单身份验证存储在会话 cookie 中的对象。
当我在 Visual Studio 中部署或调试的同一台计算机上打开浏览器时,它 100% 有效,但当我从远程计算机进行测试时,它会随机失败。我进行了一些跟踪,根据 fiddler 的说法,即使会话 Cookie 成功传递,基本上 HttpContext.Items[typeof(UserProfile)] 也为 null。
我尝试在 HTTPHandler 中实现 IReadOnlySessionState 但它仍然不起作用。我确实意识到,有时,在登录 3 分钟或更长时间后,它就 100% 有效了。
关于为什么会发生这种情况以及解决方案有什么想法吗?
谢谢
I am having trouble accessing the HttpContext.Items[typeof(UserProfile)] object sometimes when the Http Handler is called, where UserProfile is the object stored in the session cookie via forms authentication.
It works 100% of the time when I open the browser on the same machine it is being deployed or debugging in visual studio, but it fails out randomly when I test from a remote computer. I put in some tracing, and basically the HttpContext.Items[typeof(UserProfile)] is null even when the Session Cookie was passed successfully, according to fiddler.
I have tried implementing IReadOnlySessionState in the HTTPHandler but it still does not work. I do realise that sometimes, after like 3 minutes or more of logging in, it works 100% of the time after that.
Any ideas on why this might be happening and solution?
Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
会话状态、表单身份验证和 HttpContext.Items 都是不相关的 - 所以不确定为什么要将它们混合在一起。表单身份验证有自己的 cookie,与会话 cookie 分开,并且其生命周期可能不同。无论用户是否已通过身份验证,您都可以拥有会话状态。我也不确定您如何在会话 cookie 中存储
UseProfile
对象 - 建议放置一些代码。HttpContext.Items
是与每个HttpRequest
关联的属性包 - 请注意,此集合仅在请求生命周期内可用,并且没有内置机制来持久保存该集合。因此,您不太可能在此集合中获取UserProfile
对象,除非您针对某个模块或 global.asax 中的每个请求将其放在那里。您需要通过代码示例来解释您到底在做什么!Session State, Forms Authentication and
HttpContext.Items
all are unrelated - so not sure why are you mixing them up. The forms authentication has its own cookie separate from session cookie and its life time can be different. You can have session state irrespective of user has been authenticated or not. I am also not certain how you are storingUseProfile
object in the session cookie - advise to put some code.HttpContext.Items
is property bag associated with the eachHttpRequest
- note that this collection is available only for the request life time and there is no built-in mechanism to persist the collection. So its unlikely that you are going to get yourUserProfile
object in this collection unless you are putting it there for each request in some module or global.asax. You need to explain what exactly you are doing perhaps with code samples!