Tuesday, November 3, 2009

Bypass Unauthorized Certificate Consuming Web Services

When you consume any web services which uses https without by any trusted certification authority, you may encounter following exception:

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

When you try to open WSDL link in any browser, you see this warning message :

So. it's easy to override this screen accepting "Continue to this website". But in SOAP request, there is no notification screen like that. You couldn't accept it. Here is the solution for .NET, how to accept unauthorized certificate.

In your client code where SOAP request, before consuming web services, use this statement :

C# Solution
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };


VB.NET Solution
Dim delCert = Function() Return True
ServicePointManager.ServerCertificateValidationCallback = delCert


Note: Don't forget to add following namespaces : 
System.Net
System.Net.Security
System.Security.Cryptography.X509Certificates


If you check the statement, anonymous method always returns true. This means, accept all certificate regardless of their authorization.