我收到“根据验证程序,远程证书无效”异常消息,代码如下:
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 请求将毫无问题地通过。我确定这是证书的问题。有人有想法么?