2012-07-18 97 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

相关问题