微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

净WCF客户端配置htts

如何解决净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));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。