How to invoke a Web Service which requires a certi

2019-06-28 09:06发布

问题:

I need to communicate with a third party which has a .asmx web service. This web service is using https. I have the required certificate (.pfx).

When first trying to add this service using Add Service Reference in Visual Studio, I got an error. I got passed this error by importing the certificate into the Personal store. After I did that, I tried to add the Service Reference again and it works. So now I can create an instance of the web service. Nice.

But now I want to invoke the service. And when I do that I get this error:

302 Notification: Digital Certificate Missing

So how can I tell my service to use the right certificate?

回答1:

Try adding this before getting the request stream:

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
request.ProtocolVersion = HttpVersion.Version10;
request.ClientCertificates.Add(new X509Certificate2("YourPfxFile(full path).pfx", "password for your pfx file");

Depending on your security requirements and environment, you may need to use a different SecurityProrocolType value.



回答2:

I finally managed to fix my problem as follows:

var service = new Service1SoapClient();
service.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.TrustedPublisher, X509FindType.FindByIssuerName, "name_of_issuer");
((BasicHttpBinding)service.Endpoint.Binding).Security.Mode = BasicHttpSecurityMode.Transport;
((BasicHttpBinding)service.Endpoint.Binding).Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;