ASP.NET 网站 HttpContext.Items 在 HTTPHANDLER 中随机为空

发布于 2024-11-18 02:06:13 字数 430 浏览 4 评论 0原文

有时,当调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤城病女 2024-11-25 02:06:13

会话状态、表单身份验证和 HttpContext.Items 都是不相关的 - 所以不确定为什么要将它们混合在一起。表单身份验证有自己的 cookie,与会话 cookie 分开,并且其生命周期可能不同。无论用户是否已通过身份验证,您都可以拥有会话状态。我也不确定您如何在会话 cookie 中存储 UseProfile 对象 - 建议放置一些代码。

HttpContext.Items 是与每个 HttpRequest 关联的属性包 - 请注意,此集合仅在请求生命周期内可用,并且没有内置机制来持久保存该集合。因此,您不太可能在此集合中获取 UserProfile 对象,除非您针对某个模块或 global.asax 中的每个请求将其放在那里。您需要通过代码示例来解释您到底在做什么!

Session State, Forms Authentication andHttpContext.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 storing UseProfile object in the session cookie - advise to put some code.

HttpContext.Items is property bag associated with the each HttpRequest - 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 your UserProfile 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文