簡體   English   中英

WCF:將用戶名和密碼傳遞給另一個服務(無Https)

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

我必須創建一個WCF服務(ServiceWrapper),它引用另一個WCF服務(RealService)。

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

ServiceWrapper的操作調用RealService。 我需要將收到的用戶名/ passowrd傳遞給使用RealSerivce進行身份驗證,然后調用其操作。

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

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

您可以使用SOAP安全性。 有兩個SecurityMode - Message,TransportWithMessageCredential。

  1. 您應該像這樣配置<binding>部分中的安全模式(UserName)

     <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時,可以使用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; } 

如果SSL不是一個選項,則需要使用SOAP消息安全性(SecurityMode = Message)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM