我們使用Message Inspector通過在客戶端添加一些信息並在服務器端檢索添加的信息來定製SOAP消息。 我們還使用ServiceAuthorizationManager使用自定義授權管理器來使用檢索到的基於SOAP的消息信息。WCF - 如何在服務授權管理器之前調用Message Inspector?
要定製的SOAP消息,我們覆蓋兩種方法:
一個)BeforeSendRequest(客戶端) - 此方法是用於定製在消息檢查SOAP消息頭。
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
Dictionary<string,string> headerInfo = new Dictionary<string,string>();
headerInfo.Add("UserId","1111");
MessageHeader header = MessageHeader.CreateHeader("LocalName", "NamespaceURI", headerInfo);
request.Headers.Add(header);
return null;
}
b)AfterReceiveRequest(服務器端) - 此方法用於在Message Inspector中獲取自定義SOAP消息。
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
Dictionary<string, string> headerInfo = request.Headers.GetHeader<Dictionary<string, string>>("LocalName", "NamespaceURI");
return null;
}
現在,當請求從)在消息檢查客戶端然後第一呼叫在自定義授權管理器類代替AfterReceiveRequest(正在取得在服務器側進行。
我們在App.config文件中註冊了自定義的授權管理器,如圖所示:
<serviceBehaviors>
<behavior name="SampleAuthorizationService.Service1Behavior">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceAuthorization principalPermissionMode="Custom" serviceAuthorizationManagerType="SampleAuthorizationSecurity.CustomAuthorizationManager, SampleAuthorizationSecurity">
<authorizationPolicies>
<add policyType="SampleAuthorizationSecurity.CustomAuthPolicy, SampleAuthorizationSecurity"/>
</authorizationPolicies>
</serviceAuthorization>
</behavior>
</serviceBehaviors>
流量應該從消息檢查到自定義授權管理器在服務器端。但是,在我們的例子中,流程完全相反,即從自定義授權管理器到Message Inspector。 由於在App.config中註冊了自定義授權管理器,可能會發生這種情況。
任何人都可以幫我改變流從Message Inspector到服務器端的自定義授權管理器嗎?