2010-10-14 119 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标头)。