2011-02-23 49 views
1

您好,傳遞每個WCF Web服務調用的登錄信息?

我已設置了WCF作爲web服務(percall),這將web服務獲得從大範圍的系統請求。現在我們需要一些方法來識別客戶。

它可能建立一個CustomUserNamePasswordValidation,但這需要一個安全的(SLL)通信。在我們的情況下,我們不需要安全性,而且解決方案需要儘可能簡單地進行設置。

所以問題是如何發送每個電話的客戶端身份驗證(用戶名/密碼)?

我可以將識別數據放在header中,但我不知道如何通過示例soupUI來測試這種識別數據?而且我不確定是否所有可以溝通的系統都可以在沒有複雜情況下處理這個問題?

任何消耗?

請注意:我只想做1次呼叫,所以不需要使用登錄服務方法。

回答

1

WCF就不能支持發送用戶憑據不安全。爲了解決這個問題,你可以使用the clear username binding或者在郵件頭中手動添加證書(這對於WCF來說很簡單)

0

定義在象在web.config的結合:

<basicHttpBinding> 
    <binding name="BasicAuthBinding"> 
     <security mode="Message"> 
     <message clientCredentialType="UserName"/> 
     </security> 
    </binding> 
    </basicHttpBinding> 

然後定義一個服務行爲,如:

<behavior name="Namespace.TestBehaviour"> 
     <serviceCredentials> 
     <userNameAuthentication userNamePasswordValidationMode="Custom" 
      customUserNamePasswordValidatorType="Namespace.ServiceSecurity.UserAuthenticator, Namespace" /> 
     </serviceCredentials> 
     <serviceAuthorization> 
     <authorizationPolicies> 
      <add policyType="Namespace.ServiceSecurity.MyAuthorizationPolicy, Namespace" /> 
     </authorizationPolicies> 
     </serviceAuthorization> 
    </behavior> 

然後提供自定義的認證和授權類如下:

public class MyAuthorizationPolicy: IAuthorizationPolicy 
{ 
    public bool Evaluate(EvaluationContext evaluationContext, ref object state) 
    { 
     IList<IIdentity> identities = (IList<IIdentity>) evaluationContext.Properties["Identities"]; 

     foreach (IIdentity identity in identities) 
     { 
      if (identity.IsAuthenticated && 
       identity.AuthenticationType == "UserAuthenticator") 
      { 
       evaluationContext.Properties["Principal"] = identity.Name; 
       return true; 
      } 
     } 

     if (!evaluationContext.Properties.ContainsKey("Principal")) 
     { 
      evaluationContext.Properties["Principal"] = ""; 
     } 
     return false; 
    } 

    public ClaimSet Issuer 
    { 
     get { throw new NotImplementedException(); } 
    } 
} 

驗證如下:

public class UserAuthenticator : UserNamePasswordValidator 
{ 
    public override void Validate(string userName, string password) 
    { 
     //authenticate me however you want 
     //then set whatever you want 
    } 
} 

如果您需要進一步的安全性,改變basicHttpBinding的到的wsHttpBinding和使用證書

編輯:差點忘了,使用所定義的服務行爲,並在web.config中的服務接口定義綁定。

+0

>謝謝!我現在已經實現了這一點,但在瀏覽服務時得到以下異常:BasicHttp綁定要求BasicHttpBinding.Security.Message.ClientCredentialType與安全消息的BasicHttpMessageCredentialType.Certificate憑證類型相同。爲UserName憑證選擇Transport或TransportWithMessageCredential安全性。 – Banshee

+0

>我已經嘗試設置綁定到這一點:<綁定名稱=「BasicAuthIntegration」> <安全模式=「TransportCredentialOnly」> <運輸clientCredentialType =「基本」> ,這是將得到的服務會但是當試圖從測試客戶端調用服務時(使用ClientCredentials.UserName.UserName和ClientCredentials.UserName.Password),我將得到以下異常(在客戶端中):HTTP請求未經客戶端身份驗證方案「Basic」的授權。從服務器收到的驗證頭是'Basic realm ='localhost''。 – Banshee

0

在代碼:

public class WCF_Project_Authentification : UserNamePasswordValidator 
{ 
    #region UserNamePasswordValidator Interface Member 

    public override void Validate(string userName, string password) 
    { 
     if (userName != "Jeanc" || password != "fortin") 
     { 
      throw new FaultException("Authentification failed"); 
     } 
    } 
    #endregion 
} 

在配置:

<behaviors> 
     <serviceBehaviors> 
     <behavior name="Service_Behavior"> 
      <serviceMetadata httpGetEnabled="False" httpsGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="True"/> 
      <serviceCredentials> 

      <userNameAuthentication userNamePasswordValidationMode="Custom" 
            customUserNamePasswordValidatorType="WcfService.Authentification.WCF_Project_Authentification, WcfService"/> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

並在客戶端代碼:

proxy.ClientCredentials.UserName.UserName = "Jeanc"; 
proxy.ClientCredentials.UserName.Password = "fortin"; 
+0

@ Jean-Chritophe>謝謝,但我已經知道如何使用UserNamePasswordValidator,問題是我需要傳遞客戶端標識而沒有任何安全性。 – Banshee