FTP 下载文件生成错误:对 SSPI 的调用失败

Pat*_*ick 5 c# security ssl

我正在尝试从我的 FTP 服务器下载文件,但从我的生产服务器(我可以从我的笔记本电脑下载该文件)收到错误“对 SSPI 的调用失败”和内部异常“收到的消息是意外的或格式错误”。这是我的代码:

var ftpServer = "ftp://xxx.yyy.zz/";
var ftpUsername = "aaaaa";
var ftpPassword = "bbbbb";
var downloadedFilePath = "c:\\temp\\";
var downloadedFileName = "file.xls";                      
FtpWebRequest request = null;

ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;

ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
                    return true;
                };

// Get the object used to communicate with the server.
request = (FtpWebRequest)WebRequest.Create(ftpServer + "file_on_ftp_server.xls");

// download the file
request.Method = WebRequestMethods.Ftp.DownloadFile;

request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);

request.EnableSsl = true;
request.KeepAlive = false;

FtpWebResponse response = (FtpWebResponse)request.GetResponse();    // <--- ERROR

Stream responseStream = response.GetResponseStream();
FileStream writer = new FileStream(downloadedFilePath + downloadedFileName, FileMode.Create);
Run Code Online (Sandbox Code Playgroud)

Oli*_*ier 2

SSPI是一个 Windows 组件。如果您使用的是 Windows 2008,则意味着它已经很旧了。

错误“收到的消息意外或格式错误”似乎表明服务器证书使用不受支持的格式(例如,使用未知的密码),因此无法由 SSPI 层处理。

您有几种解决方案:

  • 升级Windows
  • 降级服务器证书
  • 不要使用 SSL
  • 使用不依赖 SSPI 的替代 FTP 库