2014-11-05 44 views
0

我爲我的WCF項目創建了自定義的ServiceAuthorizationManager作爲「CustomUserNamePasswordValidator」。下面是我的項目的片段。我希望我的wcf在它實際開始調用WCF API之前調用此授權類,但這不會發生。我的「登錄」WCF API正在調用這個授權類的paraller。所以當客戶端調用Login時,它同時調用 「CustomUserNamePasswordValidator」和Login方法。爲WCF定製ServiceAuthorizationManager問題

授權類

public class CustomUserNamePasswordValidator : ServiceAuthorizationManager 
{ 
    HttpRequestMessageProperty httpProperties; 
    string operationName; 

    protected override bool CheckAccessCore(OperationContext operationContext) 
    { 
     operationName = GetOperationName(operationContext); 
     httpProperties = (HttpRequestMessageProperty)operationContext.IncomingMessageProperties["httpRequest"]; 
     string authHeader = httpProperties.Headers[HttpRequestHeader.Authorization]; 

     string subno = string.Empty; 
     string password = string.Empty; 
     string version = string.Empty; 
     string credntialType = string.Empty; 

      string[] credentials = authHeader.Split(':'); 
      credntialType = credentials[0]; 

       password = credentials[1]; 

       if (!AuthorizeUser(password)) 
       { 
        throw new ArgumentException("401:Token invalid or expired.(0x000)");       
       } 
      } 
     } 

    private int AuthenticateUser(string subno, string pin, string version) 
     { 
     } 
    } 

WCF客戶端服務片段

public class ClientService : IClientService 
{ 
    public wsLoginResult LoginUser() 
    { 
      HttpRequestMessageProperty httpReqProps = (HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties["httpRequest"]; 
      string res = httpReqProps.Headers[HttpRequestHeader.Authorization]; 

      foreach (var item in res.Split(':'))     
       ActivityLog("Activity", "Login Steps", item, item); 
    } 
} 

Web.Config中摘錄

<?xml version="1.0"?> 
<configuration> 
<connectionStrings> 
<add name="wmas_subsConnectionString" connectionString="Data Source=WT;Initial Catalog=wmas;User ID=sa;Password=ra3?" providerName="System.Data.SqlClient"/> 
</connectionStrings> 
<system.web> 
    <compilation targetFramework="4.5" debug="true"/> 
    <httpRuntime targetFramework="4.5"/> 
</system.web> 
<system.serviceModel> 
    <client> 
    <endpoint address="http://192.168.1.12:7002/MobileApplicationWS/MobileApplicationApiWSImplService" 
      binding="basicHttpBinding" bindingConfiguration="MobileApplicationApiWSPortBinding" 
      contract="VASService.MobileApplicationApiWS" 
      name="MobileApplicationApiWSPort" /> 

</client> 
<services> 
    <service name="ClientService.ClientService" behaviorConfiguration="ClientService.ServiceBehavior"> 
    <endpoint address="" 
       binding="webHttpBinding" bindingConfiguration="webHttpBindingConfiguration" 
       contract="ClientService.IClientService" behaviorConfiguration="webBehaviour"/> 
    <endpoint address="stream" 
       binding="webHttpBinding" bindingConfiguration="webHttpBindingConfigurationStreamed" 
       contract="ClientService.IClientService" behaviorConfiguration="webBehaviour"/> 
    <endpoint address="mex" 
       binding="mexHttpsBinding" 
       contract="IMetadataExchange" />   
    </service> 
</services> 
<bindings> 
    <basicHttpBinding> 
    <binding name="MobileApplicationApiWSPortBinding" /> 
    </basicHttpBinding> 
    <webHttpBinding> 
    <binding name="webHttpBindingConfiguration" /> 
    <binding name="webHttpBindingConfigurationStreamed" transferMode="StreamedResponse" /> 
    </webHttpBinding> 
    </bindings> 
    <behaviors> 
    <serviceBehaviors> 
    <behavior name="ClientService.ServiceBehavior"> 
     <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
     <serviceDebug includeExceptionDetailInFaults="true"/> 
     <serviceAuthorization serviceAuthorizationManagerType="ClientService.CustomUserNamePasswordValidator, ClientService" /> 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
    <behavior name="webBehaviour"> 
     <webHttp/> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
<protocolMapping> 
    <add binding="basicHttpsBinding" scheme="https"/> 
    </protocolMapping> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> 
    </system.serviceModel> 
<system.webServer> 
<modules runAllManagedModulesForAllRequests="true"/> 
<directoryBrowse enabled="true"/> 
</system.webServer> 
    </configuration> 
+0

這是一個寧靜的服務或SOAP?請從web.config – 2014-11-05 12:24:46

+0

包含更多內容,請參閱web.config的修改後文章。 – 2014-11-05 12:36:34

回答

0

您的解決方案無法正常工作的原因是CustomUserNamePasswordValidator無法用於RESTful服務。看看:http://msdn.microsoft.com/en-us/library/aa354513(v=vs.110).aspx

該示例使用SOAP並定義激活serviceAuthorization標記的端點的行爲。如果您沒有定義端點的安全性,則serviceAuthorization根本無法工作。

<bindings> 
    <wsHttpBinding> 
    <!-- username binding --> 
    <binding name="Binding"> 
     <security mode="Message"> 
     <message clientCredentialType="UserName" /> 
     </security> 
    </binding> 
    </wsHttpBinding> 
</bindings> 

在RESTful服務沒有SecurityMode = Message,只有3個:無/運輸/ TransportCredentialOnly。請在此閱讀:http://msdn.microsoft.com/en-us/library/bb924478(v=vs.110).aspx

clientCredentialType="UserName"僅在消息中可用。

您可以嘗試定義端點安全模式以執行以下操作:將證書類型傳輸到:Basic/Certificate/Digest/None/Ntlm/Windows,但是看到您的解決方案並不能確定它們的工作方式。

如果您正在提取標題並且未使用任何「已批准」方式,則存在(我認爲)進行身份驗證的更好方法。嘗試實施擴展服務:http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iparameterinspector(v=vs.110).aspx

祝你好運!