Certificate error in .Net – The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel

You might stumble upon the error The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel when using self-signed or invalid certificates.

You may have tried all the different code patches like

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { return true; };

.. and trusting the certificate. But it does not work?

I wasted two days into this. I have often worked with invalid certificates, and just by making them trusted in e.g. the Application Pool account, the .Net code has been able to continue processing them.

But not one time. I tried all possible ways of trusting and tweaking the certificate.

I then took the time to inspect the certificate and noticed that it was using a weak MD5 algorithm that is banned by Microsoft patches. So no matter what I did, I could not get the certificate work. At least the Microsoft patch works 🙂

So if you get stuck like I did, please check that a Microsoft patch does not obstruct your code from working with the certificate. In my case I had to request the vendor a new certificate.

 

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s