2012-07-18 96 views
1

我必須創建一個引用另一個WCF服務(RealService)的WCF服務(ServiceWrapper)。WCF:將用戶名和密碼傳遞給另一個服務(無Https)

我希望ServiceWrapper的客戶端在認證請求中傳遞用戶名/密碼。

ServiceWrapper調用RealService的操作。我需要將收到的Username/passowrd傳遞給RealSerivce的Authenticate,然後調用它的Operations。

我需要在Http上託管服務而不是Https(SSL/TLS)。

問題:如何使用服務收到的客戶機Credentails在不使用Https(SSL/TLS)的情況下使用引用服務進行身份驗證?

+0

我沒有在這裏看到的問題。 – 2012-07-18 22:32:45

+0

問題:如何使用服務收到的客戶端Credentails在不使用Https(SSL/TLS)的情況下使用引用服務進行身份驗證? – Ashish 2012-07-19 19:47:10

回答

3

你可以使用SOAP安全性。有兩種SecurityModes適用於你 - Message,TransportWithMessageCredential。

  1. 你應該<binding>部分配置安全模式(用戶名)這樣

    <security mode="TransportWithMessageCredential"> 
        <transport clientCredentialType="" /> 
        <message clientCredentialType="UserName" /> 
    </security> 
    
  2. 接下來,你應該在<behavior>部分

    <behavior name="CommonBehavior"> 
        <serviceMetadata /> 
        <serviceDebug includeExceptionDetailInFaults="True"/> 
        <serviceCredentials> 
         <userNameAuthentication userNamePasswordValidationMode="Custom" 
          customUserNamePasswordValidatorType="Megatec.MasterTourService.CustomUserNameValidator, Megatec.MasterTourService"/> 
    
         <serviceCertificate findValue="WCFServer" storeLocation="LocalMachine" 
          storeName="My" x509FindType="FindBySubjectName"/> 
         <clientCertificate> 
          <authentication certificateValidationMode="PeerTrust" /> 
         </clientCertificate> 
        </serviceCredentials> 
    </behavior> 
    
  3. 指定自定義驗證程序自定義驗證您可以訪問和存儲用戶名和密碼,這些用戶名和密碼是作爲ServiceWrapper的信用提供的。

    using System.ServiceModel; 
    using System.IdentityModel.Selectors; 
    namespace MyService 
    { 
        public class CustomUserNameValidator : UserNamePasswordValidator 
        { 
         public override void Validate(string userName, string password) 
         { 
          if (!(userName == "testMan" && password == "pass")) 
           throw new FaultException("Incorrect login or password"); 
    
          // save your Usermame and Password for future usage. 
         } 
        } 
    } 
    
  4. 當你需要訪問RealService,您可以使用用戶名和密碼憑據,如下面我舉的例子:

    private readonly Dictionary<Type, Object> channelFactoryDictionary = new Dictionary<Type, Object>(); 
    private ChannelFactory<T> GetChannelFactory<T>() where T : class 
    { 
        if (channelFactoryDictionary.Keys.Contains(typeof(T))) 
         return channelFactoryDictionary[typeof(T)] as ChannelFactory<T>; 
    
        var channelFactory = new ChannelFactory<T>("*"); 
        channelFactory.Credentials.UserName.UserName = userName; 
        channelFactory.Credentials.UserName.Password = password; 
    
        channelFactoryDictionary.Add(typeof(T), channelFactory); 
    
        return channelFactory; 
    } 
    
+0

問題:如何使用服務收到的客戶端Credentails在不使用Https(SSL/TLS)的情況下使用引用服務進行身份驗證? – Ashish 2012-07-19 19:47:42

+1

+1在步驟3中,我建議將憑證存儲在自定義的OperationContext擴展中。這樣,你可以保證存儲的值具有正確的生命週期(服務調用的持續時間)。這裏有一個[示例(http://hyperthink.net/blog/a-simple-ish-approach-to-custom-context-in-wcf/「示例OperationContext擴展名」)。 – shambulator 2012-07-20 09:51:26

相關問題