In my webpart i am trying to call url to get me xml but i am getting error

The remote certificate is invalid according to the validation procedure.
`WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.`

Here is how i am trying to access it

XmlDocument doc = new XmlDocument();
List<listXml> lt = new List<listXml>();

I found this link but dont know where to put this in my webpart. It says to add this

.ServerCertificateValidationCallback += 
(sender, cert, chain, sslPolicyErrors) => true;

But i am not sure what to pass as sender,cert,chain,sslpolicyerrors. I want to avoid installing certificate on servers.

2 Answers 2


try this

ServicePointManager.ServerCertificateValidationCallback =
    delegate(object s, X509Certificate certificate,
             X509Chain chain, SslPolicyErrors sslPolicyErrors)
    { return true; };

Have you seen the another reply on the same link. He wrote a function for ignoring the bad certificate. Just call as is before your xml call.

//I use a method to ignore bad certs caused by misc errors

// after the Ignore call i can do what ever i want...
HttpWebRequest request_data = System.Net.WebRequest.Create(urlquerystring) as HttpWebRequest;

and below the Methods we are using...

/// <summary>
/// Together with the AcceptAllCertifications method right
/// below this causes to bypass errors caused by SLL-Errors.
/// </summary>
public static void IgnoreBadCertificates()
    System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

/// <summary>
/// In Short: the Method solves the Problem of broken Certificates.
/// Sometime when requesting Data and the sending Webserverconnection
/// is based on a SSL Connection, an Error is caused by Servers whoes
/// Certificate(s) have Errors. Like when the Cert is out of date
/// and much more... So at this point when calling the method,
/// this behaviour is prevented
/// </summary>
/// <param name="sender"></param>
/// <param name="certification"></param>
/// <param name="chain"></param>
/// <param name="sslPolicyErrors"></param>
/// <returns>true</returns>
private static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
    return true;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.