Behold the turtle

jb's Blog

"behold the turtle, he only makes progress when he sticks his neck out"
Dealing with invalid SSL certificates in code 03 Aug 2006 16:09

Question came up today about how to deal with code calling a web service or web site which is protected under SSL but has an invalid certificate. The code itself will be failing with a web exception that will tell you that "The remote certificate is invalid according to the validation procedure." - if you browse to the site you will recieve the standard prompt or warning about an issue with the certificate (e.g. its expired, or from a non trusted root authority). This is common practice with UAT environments which have internally issued certificates applied and the certificate issuer isnt trusted.

The best option for the latter is to install the CA's certificate so that it resolves the error with the certificate, but failing that you have a couple of code options to resolve this..

Under .NET 1/1.1 you can use the following code block to apply a custom validation routine to the certificate checking

/// <summary>

/// Provides an implementation of ICertificatePolicy which always validates.

/// used to allow HTTPS with unstrusted client certificates to work with web services

/// </summary>

internal sealed class TestingCertificatePolicy : System.Net.ICertificatePolicy

{

   public TestingCertificatePolicy() { }

   /// <summary>

   /// Always return true – this allows for untrusted certs

   /// </summary>

   public bool CheckValidationResult(System.Net.ServicePoint s, System.Security.Cryptography.X509Certificates.X509Certificate c, System.Net.WebRequest r, int i)

   {

      return true;

   }

}

Then in your calling code call:

ServicePointManager.CertificatePolicy = new TestingCertificatePolicy();

Under .NET 2.0 this has dramatically simplified through the use of an anonymous delegate:

ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {

   return true;

};

Hat tip to GangX for the .NET 2 snippet :)



Emiliano - @ 02 May 2007 06:02
It is healthy, I shall come on your site more often, thank.
SteveB - @ 31 Jul 2007 01:59
Cool - this was perfect - thanks
Rohit Jain - @ 08 Aug 2007 08:02
Awesome. Works Flawlessly. You just made my day. I had been fighting with this issue for last 1 week. Thanks a lot. Cheers, Rohit
Rohit Jain - @ 08 Aug 2007 08:02
Awesome. Works Flawlessly. You just made my day. I had been fighting with this issue for last 1 week. Thanks a lot. Cheers, Rohit
Rohit Jain - @ 08 Aug 2007 08:03
Awesome. Works Flawlessly. You just made my day. I had been fighting with this issue for last 1 week. Thanks a lot. Cheers, Rohit
Rohit Jain - @ 08 Aug 2007 08:03
Awesome. Works Flawlessly. You just made my day. I had been fighting with this issue for last 1 week. Thanks a lot. Cheers, Rohit
Rohit Jain - @ 08 Aug 2007 08:03
Awesome. Works Flawlessly. You just made my day. I had been fighting with this issue for last 1 week. Thanks a lot. Cheers, Rohit

Add a new comment:

Name:

Website:

Comment:

history


2006 (47)
2007 (79)
January 2008 (5)
February 2008 (17)
March 2008 (7)
April 2008 (4)
May 2008 (1)
June 2008 (6)
August 2008 (1)

blogs i read


JDs Weblog
Andrews Blog
Chris Auld
Josh Robb
Ivan Porto Carrero
Rod Drury
Nic Wise
Alex James
Darryl Burling (MS)
Nigel Parker (MS)
Sean McBreen (MS)

links


Mindscape
Intergen
Microsoft Regional Director Microsoft MVP - SQL Server

RSS Feed rss feed