C# WCF客户端HTTPS请求异常
我正在为.NETCore2.1中的SOAP HTTPS Web服务构建WCF客户端 服务提供商提供了一个.key和一个.cert文件,我使用openssl将其转换为一个.p12文件。通过将其添加到密钥库,我能够通过SoapUI成功地向Web服务发送请求(不需要证书以外的其他身份验证) 为了在.Net Core中执行相同的操作,我通过Visual Studio中的WCF向导向我的项目添加了一个已连接的服务。此服务基于提供的服务契约(WSDL文件)。然后,我在我的电脑上本地安装了.p12证书,并使用以下代码发出请求。“MyService”是连接的服务C# WCF客户端HTTPS请求异常,c#,.net,wcf,soap,client,C#,.net,Wcf,Soap,Client,我正在为.NETCore2.1中的SOAP HTTPS Web服务构建WCF客户端 服务提供商提供了一个.key和一个.cert文件,我使用openssl将其转换为一个.p12文件。通过将其添加到密钥库,我能够通过SoapUI成功地向Web服务发送请求(不需要证书以外的其他身份验证) 为了在.Net Core中执行相同的操作,我通过Visual Studio中的WCF向导向我的项目添加了一个已连接的服务。此服务基于提供的服务契约(WSDL文件)。然后,我在我的电脑上本地安装了.p12证书,并使用
var binding = new BasicHttpsBinding();
binding.Security.Mode = BasicHttpsSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
var endpoint = new EndpointAddress("https://x.x.x.x:8300/MyService.asmx");
var channelFactory = new ChannelFactory<MyService>(binding, endpoint);
channelFactory.Credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
channelFactory.Credentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication()
{
CertificateValidationMode = X509CertificateValidationMode.None,
RevocationMode = X509RevocationMode.NoCheck,
TrustedStoreLocation = StoreLocation.LocalMachine
};
channelFactory.Credentials.ClientCertificate.SetCertificate(
StoreLocation.CurrentUser,
StoreName.My,
X509FindType.FindByIssuerName,
"xxxxxxxxxxxxxxxxxxxxxxxxxx");
var service = channelFactory.CreateChannel();
ExecuteResponse response = service.Execute(new ExecuteRequest());
var binding=new BasicHttpsBinding();
binding.Security.Mode=basichtpssecuritymode.Transport;
binding.Security.Transport.ClientCredentialType=HttpClientCredentialType.Certificate;
binding.Security.Transport.ProxyCredentialType=HttpProxyCredentialType.None;
var endpoint=新的端点地址(“https://x.x.x.x:8300/MyService.asmx");
var channelFactory=新的channelFactory(绑定,端点);
channelFactory.Credentials.Windows.AllowedImpersonationLevel=System.Security.Principal.TokenImpersonationLevel.Impersonation;
channelFactory.Credentials.ServiceCertificate.SslCertificateAuthentication=new X509ServiceCertificateAuthentication()
{
CertificateValidationMode=X509CertificateValidationMode.None,
RevocationMode=X509RevocationMode.NoCheck,
TrustedStoreLocation=StoreLocation.LocalMachine
};
channelFactory.Credentials.ClientCertificate.SetCertificate(
StoreLocation.CurrentUser,
店名,我的,
X509FindType.FindByIssuerName,
“XXXXXXXXXXXXXXXXXXXXXX”);
var service=channelFactory.CreateChannel();
ExecuteResponse response=service.Execute(新的ExecuteRequest());
运行此代码时,我遇到以下错误:
System.ServiceModel.Security.MessageSecurityException:“HTTP请求未经客户端身份验证方案“匿名”授权。”。从服务器接收的身份验证标头为“协商”
奇怪的是,如果我使用HttpClientHandler,我就可以发出请求,这告诉我两个实现的底层结构之间一定存在不匹配。
任何知道我如何修复此错误的人?证书可能只是用于在客户端和服务器端之间建立信任关系。
为了成功调用服务,我们应该保持客户端和服务器端之间的绑定类型一致。因此,我想通过添加服务参考了解自动生成的客户端配置,请发布位于客户端项目的
appconfig
中的System.servicemodel
部分。如果服务器使用证书对客户端进行身份验证,则该错误通常表示客户端和服务器端之间尚未建立信任关系。
在客户端,我们应该在
LocalCA
中安装服务器证书。在服务器端,我们应该在LocalCA
证书存储中安装客户端证书。如果问题仍然存在,请随时通知我。该服务不是作为服务参考添加的,而是作为连接的服务添加的。我已尝试将证书安装在“中间证书颁发机构”文件夹中,但它无法解决问题。不幸的是,我无法控制服务器端,对证书的处理方式了解有限。然而,我确实相信它与特定的dotnet核心实现有关,因为我能够通过SoapUI.yeah发出请求。这些方法之间没有区别。您是否在
参考.cs
中找到了客户端配置?生成的绑定类型必须与服务器端一致。没有必要对构造函数参数进行编程。这可能与服务器端不匹配。此外,请尝试在LocalCA(受信任的根证书颁发机构)中安装这两个证书。然后,我们使用serviceclient配置服务证书和客户端证书ServiceReference1.serviceclient=new ServiceReference1.serviceclient();client.ClientCredentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.LocalMachine,StoreName.Root,X509FindType.FindByThumbprint,“cbc81f77ed01a9784a12483030ccd49f01be71c”);client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine,StoreName.My,X509FindType.FindByThumbprint,“9ee8be61d875bd6e1108c98b590386d0a489a9ca”);我想我已经得出结论,这个问题与服务器配置有关,因为我无法对此进行任何更改,所以我不得不转向另一个方向。我现在使用的是HttpClient,它显然具有与WCF服务不同的底层HTTP处理。