Monday, September 30, 2013

Could not establish trust relationship for the SSL/TLS secure channel with authority XXX

This error occurs when you are trying to consume a HTTPS webservice with self-signed certificate.

The solution is to ignore the invalid cert. (Reference from http://blog.degree.no/2012/02/accepting-invalid-ssl-certificates-programmatically-c/)
// method where request is made

// ensure SSL certificate validation uses custom method
ServicePointManager.ServerCertificateValidationCallback +=
       new RemoteCertificateValidationCallback(CustomCertificateValidatior);

// initiate a SOAP or HTTP request like normal
// and your custom method will be used for validation

// ..

// callback for validating SSL certificate during handshake
private static bool CustomCertificateValidatior(object sender, 
    X509Certificate certificate, X509Chain chain, 
    SslPolicyErrors policyErrors)
{
    // anything goes!
    return true;

    // PS: you could put your own validation logic here, 
    // through accessing the certificate properties:
    // var publicKey = certificate.GetPublicKey();
            
}

No comments:

Post a Comment