2011-04-05 109 views
2

我有一個使用net tcp綁定和自定義UserNamePasswordValidator的web服務。用戶名和密碼由客戶端在Credentials.UserName.UserName和Credentials.UserName.Password中發送。我的服務主機被設置如下:UserNamePasswordValidator和證書

host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;    host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = myCustomValidator; 
host.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.Custom; 

它完美結合,如果使用的SECURITYMODE = TransportWithMessageCredential和ClientCredentialType = MessageCredentialType.UserName但是這需要一個證書。

我想讓它在沒有證書的情況下工作。如果我將綁定的SecurityMode更改爲None,則服務啓動,但我的UserNamePasswordValidator類永遠不會被調用。

我試過SecurityMode = Message和SecurityMode.Transport,並且在這兩種情況下,服務都需要一個證書才能啓動。

我錯過了什麼嗎?自定義用戶名和密碼驗證程序是否始終需要證書?

回答

2

不幸的是WCF不允許你以純文本的形式通過線路發送用戶名/密碼。你仍然可以做到這一點,但它需要你創建一個像這個人一樣的自定義綁定here。這意味着您將不得不使用證書進行加密。請注意,這與證書認證不同,因此請不要在這方面感到困惑。所有這一切意味着你必須安裝公開密鑰的證書。所以,你的服務器配置看起來像這樣:

<bindings> 
     <wsHttpBinding> 
     <binding name="CustomAuthentication"> 
      <security mode="Message"> 
      <message clientCredentialType="UserName" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 

... 

     <serviceBehaviors> 
     <behavior name="CustomPasswordAuthentication"> 
      <serviceMetadata httpGetEnabled="true" httpGetUrl="" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
      <serviceCredentials> 
      <serviceCertificate findValue="MyCertificateName" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="Root" /> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyCompany.WebServices.CustomUsernamePasswordValidator, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors> 

客戶端:

client.ClientCredentials.UserName.UserName = "hello"; 
client.ClientCredentials.UserName.Password = "hello"; 
client.ClientCredentials.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck; 
client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust; 
client.Open(); 

希望這有助於。編輯:只需將wsHttpBinding更改爲TCP或任何您使用的。