当尝试使用FtpWebRequest c#与FTPS连接时引发异常“-对SSPI的调用失败,请参阅内部异常“;
本文关键字:异常 SSPI 失败 内部 请参阅 调用 FtpWebRequest 连接 FTPS | 更新日期: 2023-09-27 17:57:30
我正在尝试更新一些FTP代码,因为我连接的服务器现在正在强制FTPS。
我得到的例外如下:
消息=对SSPI的调用失败,请参阅内部异常。
我用来连接的代码如下:
public bool CheckFtpFile(string aFolderName, string aFileName)
{
FtpWebRequest request;
string absoluteFileName = Path.GetFileName(aFileName);
request = WebRequest.Create(new Uri(string.Format(@"ftp://{0}/{1}/{2}", ftpServer, aFolderName, absoluteFileName))) as FtpWebRequest;
request.Credentials = new NetworkCredential(userName, passWord);
request.EnableSsl = true;
request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
request.UseBinary = true;
request.UsePassive = true;
//Line added as hack
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
try
{
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
return false;
}
}
return true;
}
为了忽略证书,我使用了这个:
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
System.Diagnostics.Debug.WriteLine(certificate);
return true;
}
有人能解释一下可能导致异常的原因吗?
编辑:内部异常显示为:{"收到的消息出乎意料或格式不正确"}
我当前正在使用。Net Franework 4.0-这个问题可能与服务器被限制为TLS 1.2加密有关吗?
您必须在(FtpWebRequest)FtpWebRequest.Create(fld);
之前设置安全协议
在这里,我忽略ssl错误,并设置安全协议,然后工作。
ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11;