繁体   English   中英

net WCF 客户端配置 https

[英]net WCF client configure 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://learn.microsoft.com/en-us/do.net/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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM