WCF per connection server certificate validation

前端 未结 7 2907
清歌不尽
清歌不尽 2021-02-20 13:50

I\'m trying to bypass https certificate validation only to our own testing environment (multiple machines), while trying to keep certificate validation for all the other connect

7条回答
  •  爱一瞬间的悲伤
    2021-02-20 14:03

    What you could do is place a setting in Your web.config file.

    Then in Your code check the value in the web.config file. Then set the certificate validation depending on that value.

    Edit

    One option is:

    String url = "https://www.stackoverflow.com";
    HttpWebRequest request = HttpWebRequest.CreateHttp(url);
    request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { return true; };
    

    Another is: http://weblog.west-wind.com/posts/2011/Feb/11/HttpWebRequest-and-Ignoring-SSL-Certificate-Errors

    Edit 2

    Try something like this:

    ServicePointManager.ServerCertificateValidationCallback += customXertificateValidation;
    
    
    private static bool customXertificateValidation(object sender, X509Certificate cert, 
    X509Chain chain, SslPolicyErrors error)
    {
        if (check something here)
       {
        return true;
       }
       return false;
    }
    

提交回复
热议问题