Loading color scheme

How to fix self-signed SSL certificates error in C# REST clients

Sometimes for testing purposes, you may want to use custom self-signed HTTPS SSL certificates. But C# client application is not willing to connect to the server with a self-signed certificate, throwing the error "Could not create SSL/TLS secure channel".

For testing purposes, you can use the following solution, which allows all SSL certificates to be correctly validated. Just add the following code to your Main method or Form_Load method:

 

System.Net.ServicePointManager.ServerCertificateValidationCallback = (s, ce, ca, p) => true;

 

 

ServicePointManager.ServerCertificateValidationCallback is a function, that is used to validate a server certificate. Our application uses custom validation by the client. When doing custom validation, the sender parameter passed to the callback can be a host string name or an object derived from WebRequest (HttpWebRequest, for example) depending on the CertificatePolicy property.

We just return true for any certificate, because we are testing.

While it is OK for development purposes, for the production app this may lead to a security hole. To make your app more secure, consider writing a more sophisticated ServerCertificateValidationCallback which does better validation, add your certificate to a trusted store on the client computer, or use a trusted authority-signed certificate.

Further reading: MSDN

Get all interesting articles to your inbox
Please wait