HOWTO: Get rid of " Could Not Establish Trust Relationship with Remote Server" error


If you are getting this error ” Could Not Establish Trust Relationship with Remote Server” when trying to access a webservice secured by SSL, then read on. One possible issue might be that you do not have a SSL Certificate isssued by a trusted Certification Authority(CA). (Another case of error is explained in this Microsoft Article: http://support.microsoft.com/?scid=kb;en-us;823177). For both these instances workaround given in MS article is a possible solution. It is simple to implement, add this class that implements ICertificatePolicy and add this line of code before you call your web service.

System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();

Code for the class is given below,

using System.Net;
using System.Security.Cryptography.X509Certificates;

public class MyPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint
, X509Certificate certificate
, WebRequest request
, int certificateProblem) {

//Return True to force the certificate to be accepted.
return true;

} // end CheckValidationResult
} // class MyPolicy

You could also check for specific errors/warnings in the CheckValidationResult method, in the above given example, it overrides any error!

Happy coding:)

One comment

  1. Anonymous

    Hi there

    Sheesh…thanks so much. I’ve seen countless pages saying “Just inherit from ICertificatePolicy and ignore the cert” but NONE of them actually had that crucial extra bit of info:
    System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();

    AAARGGG!

    Thanks again
    Cheers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s