Webservice(s) via HTTPS - Trust Relationship Using ICertificatePolicy
If you ever tried to consume a web resource progrmmatically via SSL/HTTPS it is possible that you would get the following error:
"The underlying connection was closed: Could not establish trust relationship with remote server."
The problem is that your application does not accept certificates that the remote service sends in order to establish connection. If you browse the web via HTTPS (SSL), sometimes you could see that annoying HTTPS confirmation dialog. When you don't browse it manually but try to establish the connection programmatically - you just have to simulate that dialog.
All credit for the solution goes to Jan Tielens. System.Net namespace in .NET Framework provides ICertificatePolicy interface with a single method called CheckValidationResult, so we can decide whether we trust the certificate or not by overloading this method. The most simple implementation would be the following "acceptall" class:
public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy { public TrustAllCertificatePolicy() {} public bool CheckValidationResult(ServicePoint sp, X509Certificate cert, WebRequest req, int problem) { return true; } }
So before establishing HTTPS connection (either via WebRequest, WebServices or other) to remote server just call:
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
Enjoy
Saturday, May 1, 2004 2:49 AM