我正在构建一个WebApi应用程序,该应用程序需要通过验证发送的HTTP标头中的凭据来对每个请求进行身份验证。
我希望为此使用HttpModule,但是当我调试时,只有在启动应用程序时才会点击该模块。我可以在控制器操作中主动触发请求,它永远不会命中HttpModule(因此永远不会检查凭据)。
我已经在web.config中激活了HttpModule,如下所示:
<system.webServer>
<modules>
<add name="BasicAuthHttpModule" type="MyAssembly.Modules.BasicAuthHttpModule"/>
</modules>
为什么当我调用控制器时模块无法运行-此方法不适用于经过身份验证的WebApi应用程序吗?我不能使用IIS身份验证。
如果您的目标是确保每个传入的Http请求都具有适当的BasicAuth标头,请尝试将runAllManagedModulesForAllRequests属性添加到配置文件中的模块:
<modules runAllManagedModulesForAllRequests="true">
<add name="BasicAuthHttpModule" type="MyAssembly.Modules.BasicAuthHttpModule"/>
</modules>
一个更合适的解决方案是使用操作过滤器进行授权:
http://www.ryadel.com/cn/http-basic-authentication-asp-net-mvc-using-custom-actionfilter/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句