C# Ignore certificate errors?

后端 未结 11 1025
鱼传尺愫
鱼传尺愫 2020-11-22 15:01

I am getting the following error during a web service request to a remote web service:

Could not establish trust relationship for the SSL/TLS secure c

11条回答
  •  清酒与你
    2020-11-22 15:50

    IgnoreBadCertificates Method:

    //I use a method to ignore bad certs caused by misc errors
    IgnoreBadCertificates();
    
    // 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...
    */
    
    /// 
    /// Together with the AcceptAllCertifications method right
    /// below this causes to bypass errors caused by SLL-Errors.
    /// 
    public static void IgnoreBadCertificates()
    {
        System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
    }  
    
    /// 
    /// 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
    /// 
    /// 
    /// 
    /// 
    /// 
    /// true
    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;
    } 
    

提交回复
热议问题