我正在尝试为在 WCF 中实现并托管在 Azure 上的 REST 服务实施身份验证。我正在使用 HttpModule 来处理 AuthenticationRequest、PostAuthenticationRequest 和 EndRequest 事件。如果缺少 Authorization 标头或其中包含的令牌无效,则在 EndRequest 期间,我将 Response 上的 StatusCode 设置为 401。但是,我确定 EndRequest 被调用了两次,并且在第二次调用时,响应已经具有标头设置,导致设置 StatusCode 的代码抛出异常。
我在 Init() 中添加了锁,以确保处理程序没有被注册两次;还是跑了两次。Init() 也运行了两次,表明正在创建 HttpModule 的两个实例。但是,在 VS 调试器中使用 Set Object ID 似乎表明请求实际上是不同的请求。我已经在 Fiddler 中验证了浏览器只向我的服务发出了一个请求。
如果我切换到使用 global.asax 路由而不是依赖于 WCF 服务主机配置,则处理程序只调用一次并且一切正常。
如果我将配置添加到 system.web 配置部分以及 Web.config 中的 system.webServer 配置部分,则处理程序只调用一次,一切正常。
所以我有缓解措施,但我真的不喜欢我不理解的行为。为什么处理程序被调用两次?
这是该问题的最小重现:
网络配置:
<system.web>
<compilation debug="true" targetFramework="4.0" />
<!--<httpModules>
<add name="AuthModule" type="TestWCFRole.AuthModule, TestWCFRole"/>
</httpModules>-->
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<services>
<service name="TestWCFRole.Service1">
<endpoint binding="webHttpBinding" name="RestEndpoint" contract="TestWCFRole.IService1" bindingConfiguration="HttpSecurityBinding" behaviorConfiguration="WebBehavior"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost/" />
</baseAddresses>
</host>
</service>
</services>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
<bindings>
<webHttpBinding>
<binding name="HttpSecurityBinding" >
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="AuthModule" type="TestWCFRole.AuthModule, TestWCFRole"/>
</modules>
<directoryBrowse enabled="true"/>
</system.webServer>
Http模块:
using System;
using System.Web;
namespace TestWCFRole
{
public class AuthModule : IHttpModule
{
/// <summary>
/// You will need to configure this module in the web.config file of your
/// web and register it with IIS before being able to use it. For more information
/// see the following link: http://go.microsoft.com/?linkid=8101007
/// </summary>
#region IHttpModule Members
public void Dispose()
{
//clean-up code here.
}
public void Init(HttpApplication context)
{
// Below is an example of how you can handle LogRequest event and provide
// custom logging implementation for it
context.EndRequest += new EventHandler(OnEndRequest);
}
#endregion
public void OnEndRequest(Object source, EventArgs e)
{
HttpContext.Current.Response.StatusCode = 401;
}
}
}