今天寫程序的時候調用到一個第三方的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();