简体   繁体   English

net WCF 客户端配置 https

[英]net WCF client configure htts

I am trying to connect to a web service 'https://trackingqa.estafeta.com/Service.asmx' programaticaly what i mean is with code no configuration (.config) I try this我正在尝试以编程方式连接到 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);

but i get this error:但我收到此错误:

The client certificate is not provided.未提供客户端证书。 Specify a client certificate in ClientCredentials.在 ClientCredentials 中指定客户端证书。

i found this doc on MSDN ( https://learn.microsoft.com/en-us/do.net/framework/wcf/feature-details/transport-security-with-certificate-authentication#configure-the-client )我在 MSDN 上找到了这个文档( https://learn.microsoft.com/en-us/do.net/framework/wcf/feature-details/transport-security-with-certificate-authentication#configure-the-client

and i try this:我试试这个:

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

but i getting this error:但我收到此错误:

Exception: Cannot find X.509 certificate with the following search criteria: StoreName 'My', StoreLocation 'CurrentUser', FindType 'FindBySubjectName', FindValue 'TrackingQA.estafeta.com'.异常:找不到具有以下搜索条件的 X.509 证书:StoreName“My”、StoreLocation“CurrentUser”、FindType“FindBySubjectName”、FindValue“TrackingQA.estafeta.com”。

my question is how configure de endpoint in the client我的问题是如何在客户端配置端点

I found a solution., Works for me.我找到了解决方案。对我有用。

  1. Set security protocol设置安全协议

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

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

complete code:完整代码:

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