Web API 使用 HttpModule 进行身份验证
Web API Authentication with HttpModule
我正在构建一个 WebApi 应用程序,它需要通过验证来自 HTTP header 发送的凭据来对每个请求进行身份验证。
我希望为此使用 HttpModule,但是当我调试时,只有在我启动应用程序时才会命中该模块。我可以在控制器操作中主动触发请求,它永远不会命中 HttpModule(因此永远不会检查凭据)。
我已经在 web.config 中激活了 HttpModule,如下所示:
<system.webServer>
<modules>
<add name="BasicAuthHttpModule" type="MyAssembly.Modules.BasicAuthHttpModule"/>
</modules>
为什么当我调用控制器时模块没有 运行 - 这种方法是否不适用于经过身份验证的 WebApi 应用程序?我无法使用 IIS 身份验证。
如果您的目标是确保每个传入的 Http 请求都具有适当的 BasicAuth headers,请尝试将 runAllManagedModulesForAllRequests 属性添加到配置文件中的模块:
<modules runAllManagedModulesForAllRequests="true">
<add name="BasicAuthHttpModule" type="MyAssembly.Modules.BasicAuthHttpModule"/>
</modules>
更合适的解决方案是使用 Action Filter 进行授权:
http://www.ryadel.com/en/http-basic-authentication-asp-net-mvc-using-custom-actionfilter/
我正在构建一个 WebApi 应用程序,它需要通过验证来自 HTTP header 发送的凭据来对每个请求进行身份验证。
我希望为此使用 HttpModule,但是当我调试时,只有在我启动应用程序时才会命中该模块。我可以在控制器操作中主动触发请求,它永远不会命中 HttpModule(因此永远不会检查凭据)。
我已经在 web.config 中激活了 HttpModule,如下所示:
<system.webServer>
<modules>
<add name="BasicAuthHttpModule" type="MyAssembly.Modules.BasicAuthHttpModule"/>
</modules>
为什么当我调用控制器时模块没有 运行 - 这种方法是否不适用于经过身份验证的 WebApi 应用程序?我无法使用 IIS 身份验证。
如果您的目标是确保每个传入的 Http 请求都具有适当的 BasicAuth headers,请尝试将 runAllManagedModulesForAllRequests 属性添加到配置文件中的模块:
<modules runAllManagedModulesForAllRequests="true">
<add name="BasicAuthHttpModule" type="MyAssembly.Modules.BasicAuthHttpModule"/>
</modules>
更合适的解决方案是使用 Action Filter 进行授权:
http://www.ryadel.com/en/http-basic-authentication-asp-net-mvc-using-custom-actionfilter/