Visual studio 具有ssl和用户名/密码身份验证的WCF客户端

Visual studio 具有ssl和用户名/密码身份验证的WCF客户端,visual-studio,wcf,authentication,ssl,Visual Studio,Wcf,Authentication,Ssl,我正在尝试连接到可通过https访问且需要身份验证的服务 我得到的错误是: 试验方法 TestProject1.UnitTest1.TestReharB2B 引发异常: System.ServiceModel.Security.MessageSecurityException: HTTP请求未经授权 客户端身份验证方案 “匿名”。身份验证标头 从服务器接收的是“基本” realm=“Application””。--> System.Net.WebException:远程 服务器返回错误:(401

我正在尝试连接到可通过https访问且需要身份验证的服务

我得到的错误是:

试验方法 TestProject1.UnitTest1.TestReharB2B 引发异常: System.ServiceModel.Security.MessageSecurityException: HTTP请求未经授权 客户端身份验证方案 “匿名”。身份验证标头 从服务器接收的是“基本” realm=“Application””。--> System.Net.WebException:远程 服务器返回错误:(401) 未经授权

在我看来,用户名和密码似乎没有发送到服务。我错过了什么

守则:

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();

您的服务返回错误,因为服务器和客户端之间存在一些错误配置。您的客户端将传输安全性与用户名消息凭据一起使用。这意味着使用HTTPS,消息包含带有用户名和密码的SOAP头。但您的服务返回异常,因为它(可能是IIS)希望使用基本凭据实现传输安全性。这意味着HTTPS和HTTP基本身份验证(HTTP头)