C#: Suppressing Certificate Errors in Code

Normally when access a page over ssl via a browser, you will get a dialog box asking if you wish to proceed if there are any certificate errors. If, however, you are calling the code via the object model, you do not get prompted – instead an exception is thrown. To mitigate for this, it is possible to specify a delegate method to validate certificates – the example below ignores all certificate errors (requires a reference to the System.Security.Cryptography.X509Certificates namespace to match the delegate signature):

public static bool TrustAllCertsValidate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
return true;

To use this and ignore certificate errors, add a reference to the System.Net namespace and add the following line before accessing your web resource (e.g. web service).

ServicePointManager.ServerCertificateValidationCallback += TrustAllCertsValidate;

You May Also Like

About the Author: rnowik

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.