Bypass invalid SSL certificate errors when calling web services in .Net

前端 未结 8 878
栀梦
栀梦 2020-11-28 20:33

We are setting up a new SharePoint for which we don\'t have a valid SSL certificate yet. I would like to call the Lists web service on it to retrieve some meta data about th

8条回答
  •  清歌不尽
    2020-11-28 21:12

    I solved it this way:

    Call the following just before calling your ssl webservice that cause that error:

    using System.Net;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    
    /// 
    /// solution for exception
    /// System.Net.WebException: 
    /// The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
    /// 
    public static void BypassCertificateError()
    {
        ServicePointManager.ServerCertificateValidationCallback +=
    
            delegate(
                Object sender1,
                X509Certificate certificate,
                X509Chain chain,
                SslPolicyErrors sslPolicyErrors)
            {
                return true;
            };
    }
    

提交回复
热议问题