首页 > 解决方案 > net 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'。

我的问题是如何在客户端配置 de 端点

标签: .netweb-serviceswcfwebservice-client

解决方案


我找到了解决方案。,对我有用。

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

推荐阅读