![](/img/trans.png)
[英]WCF: Is Authentication / Authorization inside a message inspector a good design?
[英]WCF - How to invoke Message Inspector before Service Authorization Manager?
我們使用Message Inspector通過在客戶端添加一些信息並在服務器端檢索添加的信息來自定義SOAP消息。 我們還使用自定義授權管理器,使用ServiceAuthorizationManager來使用檢索到的基於SOAP的消息信息。
要自定義SOAP消息,我們將覆蓋兩種方法:
a)BeforeSendRequest(客戶端) - 此方法用於在Message Inspector中自定義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;
}
現在,當從客戶端發出請求時,將在自定義授權管理器類中進行第一次調用,而不是在服務器端的Message Inspector中進行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到服務器端的Custom Authorization Manager。 但是,在我們的例子中,流程正好相反,即從Custom Authorization Manager到Message Inspector。 由於在App.config中注冊自定義授權管理器,可能會發生這種情況。
任何人都可以幫我改變從服務器端的Message Inspector到Custom Authorization Manager的流程嗎?
我找不到在Service Authorization Manager之前調用Message Inspector的方法,所以我通過在Service Authorization Manager中重新定義方法CheckAccess(OperationContext operationContext, ref Message message)
並在此方法中完成所有工作來解決了這個問題。 這不是一個漂亮的解決方案,但它做的工作:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.