我得到一個用下面的代碼異常消息「的遠程證書根據驗證程序無效」:的FtpWebRequest EnableSSL錯誤
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(MyCertValidationCb);
var request = (FtpWebRequest)WebRequest.Create(new Uri(myUri));
request.EnableSsl = true;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.BeginGetRequestStream(EndGetStreamCallback, _state);
public static bool MyCertValidationCb(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors)
== SslPolicyErrors.RemoteCertificateChainErrors)
{
return false;
}
if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNameMismatch)
== SslPolicyErrors.RemoteCertificateNameMismatch)
{
Zone z;
z = Zone.CreateFromUrl(((FtpWebRequest)sender).RequestUri.ToString());
if (z.SecurityZone == SecurityZone.Intranet
|| z.SecurityZone == SecurityZone.MyComputer)
{
return true;
}
return false;
}
return false;
}
FTP服務器FileZilla的。啓用基於SSL的FTP,並啓用允許通過TLS的顯式FTP。我生成了一個certificate.crt文件。使用filezilla客戶端連接到ftp位置,並在彈出窗口中選中「始終信任此證書」。
在MyCertValidationCb方法,(sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors)== SslPolicyErrors.RemoteCertificateChainErrors是總是如此。
如果我將MyCertValidationCb更改爲始終返回true,那麼ftp請求將毫無問題地通過。我相信這是證書的問題。有人有主意嗎?