2010-10-14 156 views
1

我想連接到服務,這是通過https訪問和需要身份驗證。WCF客戶端與SSL和用戶名/密碼認證

我得到的錯誤是:

Test method TestProject1.UnitTest1.TestReharB2B threw exception: System.ServiceModel.Security.MessageSecurityException: The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="Application"'. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized..

在我看來,這樣的用戶名和密碼不會被髮送到服務。我錯過了什麼?

代碼:因爲服務器和客戶端之間的一些錯誤配置

EndpointAddress address = new EndpointAddress(
        new Uri("https://84.52.158.151:8443/JavaStore/services/B2BService"), 
         EndpointIdentity.CreateDnsIdentity("JavaStore"), 
         new AddressHeaderCollection() 
       ); 

BasicHttpBinding binding = new BasicHttpBinding(); 
binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential; 
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName; 

var client = new ReharB2BService.B2BServicePortTypeClient(binding, address); 

client.ChannelFactory.Credentials.UserName.UserName = "dotNet"; 
client.ChannelFactory.Credentials.UserName.Password = "dotnetpwd"; 

client.Open(); 
client.getAllItems(); 

回答

2

你的服務返回錯誤。您的客戶端使用具有UserName消息憑據的傳輸安全性。這意味着使用HTTPS並且消息包含帶有用戶名和密碼的SOAP頭。但是,您的服務會返回異常,因爲它(可能是IIS)期望使用基本憑據進行傳輸安全性。這意味着HTTPS和HTTP基本認證(HTTP標頭)。