WCFに実装され、AzureでホストされているRESTサービスの認証を実装しようとしています。HttpModuleを使用して、AuthenticationRequest、PostAuthenticationRequest、およびEndRequestイベントを処理しています。Authorizationヘッダーがない場合、またはそこに含まれるトークンが無効である場合、EndRequest中にResponseのStatusCodeを401に設定しています。ただし、EndRequestが2回呼び出され、2回目の呼び出しで応答にすでにヘッダーが含まれていると判断しました。 setを実行すると、StatusCodeを設定するコードが例外をスローします。
Init()にロックを追加して、ハンドラーが2回登録されないようにしました。まだ2回実行されました。Init()も2回実行され、HttpModuleの2つのインスタンスが作成されていることを示しています。ただし、VSデバッガーでSet Object IDを使用すると、要求が実際には異なる要求であることが示されているようです。Fiddlerで、ブラウザーからサービスに発行されているリクエストが1つしかないことを確認しました。
WCFサービスホストの構成に依存する代わりにglobal.asaxルーティングを使用するように切り替えると、ハンドラーは1回だけ呼び出され、すべてが正常に機能します。
system.web構成セクションとWeb.configのsystem.webServer構成セクションに構成を追加すると、ハンドラーは1回だけ呼び出され、すべてが正常に機能します。
ですから、私には緩和策がありますが、私は理解できない行動が本当に嫌いです。ハンドラーが2回呼び出されるのはなぜですか?
問題の最小限の再現は次のとおりです。
Web.config:
<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;
}
}
}