简体   繁体   中英

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

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.

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 )

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'.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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