净WCF客户端配置htts

问题描述

我正在尝试连接到Web服务'https://trackingqa.estafeta.com/Service.asmx'编程,我的意思是代码无配置(.config) 我试试这个

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
        EndpointAddress endpoint = new EndpointAddress(url);
        ServiceSoapClient client = new ServiceSoapClient(binding,endpoint);

但是我得到这个错误

未提供客户端证书。在ClientCredentials中指定客户端证书。

我在MSDN(https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/transport-security-with-certificate-authentication#configure-the-client)上找到了此文档

我尝试这个:

client.ClientCredentials.ClientCertificate.SetCertificate(
            System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser,System.Security.Cryptography.X509Certificates.StoreName.My,System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName,"TrackingQA.estafeta.com");

但我收到此错误

异常:找不到具有以下搜索条件的X.509证书:StoreName'My',StoreLocation'CurrentUser',FindType'FindBySubjectName',FindValue'TrackingQA.estafeta.com'。

我的问题是如何在客户端中配置终结点

解决方法

我找到了解决方案。 为我工作。

  1. 设置安全协议

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    
  2. 设置证书

     client.ClientCredentials.ClientCertificate.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(Convert.FromBase64String(StringCertificateBase64));
    

完整代码:

var binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
        EndpointAddress endpoint = new EndpointAddress(url);
        ServiceSoapClient client= new ServiceSoapClient(binding,endpoint);
        client.ClientCredentials.ClientCertificate.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(Convert.FromBase64String(StringCertificateBase64));