如何从 IHttpModule 获取数据到 ASP.NET MVC RequestContext?

发布于 2024-07-11 06:42:50 字数 529 浏览 8 评论 0原文

我有一个自定义 IHttpModule 来进行身份验证(到 Facebook、Myspace 等)。 它引发一个可以在 HttpApplication 实例中处理的事件,其中包含有关用户的详细信息。 它很像 FormsAuthenticationModule 引发的 OnAuthenticate 事件。

我应该将从 HttpApplication 类获取的信息放在哪里,以便 ASP.NET MVC 管道可以使用它? 我曾经把它放在 Context.Items 字典中。 但似乎对于(更友好的)HttpContextBase,我想把它放在那里,以便很容易模拟/注入,并且我没有在控制器中使用 HttpContext.Current 。

那么,基本上,如何最好地将数据从 IHttpModule 获取到 MVC 上下文中? 快速浏览后我能想到的最好方法是创建一个自定义 IControllerFactory 以从 HttpContext 中提取数据并将其添加到 RequestContext 中。 我回答了我自己的问题吗? 有没有一种侵入性较小的方式(比如我可以处理的事件)?

I've got a custom IHttpModule that does authentication (to Facebook, Myspace, etc). It raises an event that can be handled in the HttpApplication instance with details about the user. It's a lot like the OnAuthenticate event that the FormsAuthenticationModule raises.

From the HttpApplication class, where should I place this information I get so it is available to the ASP.NET MVC pipeline? I used to stick it in the Context.Items dictionary. But it seems with the (much friendlier) HttpContextBase I want to put it there so that it's easy to mock/inject and I'm not using HttpContext.Current in my controller.

So, basically, how do I best get data from an IHttpModule into the MVC context? The best I could come up with from a quick look is to create a custom IControllerFactory to pull data out of the HttpContext and add it to the RequestContext. Did I answer my own question? Is there a less intrusive way (like an event I can handle)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

风铃鹿 2024-07-18 06:42:50

我的猜测是你的模块在路由之前运行。 如果是这样,那么直接将其添加到 RequestContext 还为时过早。

我会尝试将其添加到 HttpContext.Current.Items 中。 路由将使用 HttpContextBase 包装该 HttpContext。 然后您应该能够通过 RequestContext.HttpContext.Items 从任何地方访问它。

My guess is that your module runs before Routing. If so, then it's too early to add it to the RequestContext directly.

I would try adding it to HttpContext.Current.Items. Routing will wrap that HttpContext with an HttpContextBase. You should then be able to access it from anywhere via RequestContext.HttpContext.Items.

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