Elahe
Elahe

Reputation: 1399

How to set certificate in C# Win Application

Im using a webservice in my program.

this is the web service : "https://X.Y.Z.W/ib/ws/openbill.asmx?wsdl"

It works as local. when I write this on Internet Explorer, I can see the proper page after selecting "Continue to this website (not recommended)" in this attachment : enter image description here

but when I want to access to this, in my c# code, I get this exception :

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

So how can I access this in my c# program?

Upvotes: 1

Views: 859

Answers (2)

Yacoub Massad
Yacoub Massad

Reputation: 27861

The web service that you are trying to connect to is using SSL/TLS. When you open the web service via internet explorer, it is giving you a warning that it cannot validate the certificate of the web service.

This has many reasons, and I guess in your case it is that the certificate that the web service is using is not for X.Y.Z.W.

Another reason could be that your machine does not trust the root issuer of the web service certificate. But from the error message that you have, I don't think this is the case.

You can view the certificate in IE by click on "Continue on this web site..", and the clicking on "Certificate Error", and the "View certificates".

enter image description here

From there, you will view the certificate. Go to details, and look for DNS Name inside Subject Alternative Name.

The DNS name is the name of the machine that the web service certificate was given for.

enter image description here

I guess in your case it will not be X.Y.Z.W.

Is the web service yours? can you obtain a different certificate for it? If so, can make sure you create a certificate that has the correct DNS name.

As a last resort, you can skip certificate validation from your code (this is not recommended). Take a look at this question.

Upvotes: 0

Hamed
Hamed

Reputation: 1213

I think you should first acquire a certificate file, and then use it in your code. For example, to set it for a SMTPClient object:

string certificate = "Certificate.cer";

X509Certificate cert = new X509Certificate2(certificate);

MailMessage message = new MailMessage(from, to);

SmtpClient client = new SmtpClient(server);

client.ClientCertificates.Add(cert);

client.Send(message);

Upvotes: 1

Related Questions