System.Net.CertificatePolicy to ServerCertificateValidationCallback Accept all certificate policies

后端 未结 2 1276
广开言路
广开言路 2020-12-30 07:25

I\'ve downloaded some sample code that is a bit outdated. It has the following class:

public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
         


        
相关标签:
2条回答
  • 2020-12-30 08:08

    Include the following class in your code

     public static class SSLValidator
            {
                private static bool OnValidateCertificate(object sender, X509Certificate certificate, X509Chain chain,
                                                          SslPolicyErrors sslPolicyErrors)
                {
                    return true;
                }
                public static void OverrideValidation()
                {
                    ServicePointManager.ServerCertificateValidationCallback =
                        OnValidateCertificate;
                    ServicePointManager.Expect100Continue = true;
                }
            }
    

    Then call the following before you make service call but be careful to remove this code on the production when you have real certs

    SSLValidator.OverrideValidation();  
    

    Or you can do the following to use it only for debugging

    #if DEBUG
    
                SSLValidator.OverrideValidation();
    #endif 
    
    0 讨论(0)
  • 2020-12-30 08:24

    I use the following when connecting to other web services.

    //workaround for SSL certificate issue
    ServicePointManager.ServerCertificateValidationCallback = 
      (sender, certificate, chain, sslPolicyErrors) => { return true; };
    

    per comments I need to add to the blurb - DO NOT DO THIS IN PRODUCTION (if you do - please send $500 to my paypal account)

    0 讨论(0)
提交回复
热议问题