C#: Suppressing Certificate Errors in Code

11 March 2008

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;



permalink   (currently 0 comments)

Add A Comment

Your Name:
* Please enter the code: Captcha Image
   

* Please enter your message: