来对远程X.509证书的验证

时间:2022-05-02 01:39:10

今天写措施的时候挪用到一个第三方的DLL文件,本机调试一切都正常,但是措施不是随处事器以后一直提示一个BUG:"根本连接已经*: 未能为SSL/TLS 安适通道成立信任关系"。
后来把DLL文件进行反编译,,发明是在获得请求的时候堕落了。

引用  WebResponse response = WebRequest.Create("https://……").GetResponse();

界说一个类,来对长途X.509证书的验证,进行措置惩罚惩罚,返回为true.我们要本身界说一个类,然后在客户单挪用WCF处事之前,执行一次即可。代码如下:

public static class Util { /// <summary> /// Sets the cert policy. /// </summary> public static void SetCertificatePolicy() { ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate; } /// <summary> /// Remotes the certificate validate. /// </summary> private static bool RemoteCertificateValidate( object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) { // trust any certificate!!! System.Console.WriteLine("Warning, trust any certificate"); return true; } }

在WebRequest.Create("https://……").GetResponse();挪用操纵点前先挪用这个要领: Util.SetCertificatePolicy();

标签:

原文地点:https://www.cnblogs.com/xingyadian/p/8970516.html