简体   繁体   English

WCF:将用户名和密码传递给另一个服务(无Https)

[英]WCF: Passing Username and password to another service (No Https)

I have to Create a WCF service (ServiceWrapper), which references another WCF Service(RealService). 我必须创建一个WCF服务(ServiceWrapper),它引用另一个WCF服务(RealService)。

I want Clients of ServiceWrapper to pass Username/password in authentication request. 我希望ServiceWrapper的客户端在身份验证请求中传递用户名/密码。

The Operations of ServiceWrapper Call RealService. ServiceWrapper的操作调用RealService。 I need to pass the recieved Username/passowrd to Authenticate with RealSerivce and then call its Operations. 我需要将收到的用户名/ passowrd传递给使用RealSerivce进行身份验证,然后调用其操作。

I need to host the service on Http and not Https(SSL/TLS). 我需要在Http而不是Https(SSL / TLS)上托管服务。

Question: How to use the Client Credentails Recieved by a Service to authenticate with a Referenced Service without using Https(SSL/TLS)? 问题:如何使用服务接收的客户端Credentails在不使用Https(SSL / TLS)的情况下使用引用服务进行身份验证?

Your can use SOAP security. 您可以使用SOAP安全性。 There are two SecurityModes for you - Message, TransportWithMessageCredential. 有两个SecurityMode - Message,TransportWithMessageCredential。

  1. You should configure security mode (UserName) in <binding> section like this 您应该像这样配置<binding>部分中的安全模式(UserName)

     <security mode="TransportWithMessageCredential"> <transport clientCredentialType="" /> <message clientCredentialType="UserName" /> </security> 
  2. Next, you should specify custom validator in <behavior> section 接下来,您应该在<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. In your custom validator you can access and store user name and password, which were given as creditionals for ServiceWrapper. 在您的自定义验证器中,您可以访问和存储用户名和密码,这些用户名和密码作为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. When you need to access RealService, you can use userName and password as credentials, like my example below: 当您需要访问RealService时,可以使用userName和password作为凭据,如下例所示:

     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; } 

If SSL is not an option, you will need to use SOAP message security (SecurityMode = Message). 如果SSL不是一个选项,则需要使用SOAP消息安全性(SecurityMode = Message)。

http://msdn.microsoft.com/en-us/library/ms733137.aspx http://msdn.microsoft.com/en-us/library/ms733137.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM