【问题标题】:FtpWebRequest with EnableSSL带有 EnableSSL 的 FtpWebRequest
【发布时间】:2009-10-22 15:29:27
【问题描述】:

我实现了我的自定义 FTP 类来与我付费购买的托管服务器一起工作。 我使用 FTP 来备份、恢复和更新我的应用程序。 我现在正要启用 ssl 将其投入生产。 我问过我的托管公司是否支持 ssl 协议,他们说支持。

所以我在 Microsoft MSDN 教程之后修改了我的方法,如下所示:

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(m_ftpAddress.Trim()));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(m_ftpUsername, m_ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
                X509Certificate cert = new X509Certificate(path to a certificate created with makecert.exe);

reqFTP.ClientCertificates.Add(cert);
reqFTP.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
reqFTP.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Identification;

reqFTP.EnableSsl = true;

现在,MSDN 表示,如果服务器支持 ssl 协议,则在使用 AUTH TLS 询问时它不会抛出异常。这可以在跟踪日志中看到。所以我想这不是服务器问题。

在认证阶段之后,服务器返回一个

System.Net 信息:0 : [0216] FtpControlStream#41622463 - 收到响应 [227 进入被动模式(IP 和端口号)。]

触发错误的消息:

System.Net 错误:0 : [0216] FtpWebRequest#12547953::GetResponse 中的异常 - 远程服务器返回错误:227 进入被动模式(IP 和端口号)。

我试过设置

reqFTP.UsePassive = true;

属性为假,然后我得到这个错误:

System.Net 信息:0 : [2692] FtpControlStream#4878312 - 收到响应 [500 Illegal PORT command]

当然,如果不将 EnableSLL 属性设置为 true,则一切正常。

有人对此有任何想法吗?

编辑:我将代码修改为休闲:

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(m_ftpAddress.Trim()));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(m_ftpUsername, m_ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

ServicePointManager.ServerCertificateValidationCallback = new       
     System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);

reqFTP.ClientCertificates.Add(cert);
reqFTP.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
reqFTP.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Identification
reqFTP.EnableSsl = true;

ValidateServerCertificate 总是返回 true。在此修改后,效果是没有的。

我不明白:

  • 此时应用程序正在使用服务器证书,对吧?
  • 在修改之前也使用我的?

有人可以解释一下我的工作原理吗?

编辑:在与托管公司交换了许多电子邮件后,事实证明他们的 FTP 软件有问题,而代码也没有问题。

【问题讨论】:

  • 然后发布您的编辑作为答案并接受它。

标签: c# ssl ftp ftpwebrequest


【解决方案1】:

您是否检查过您的主机使用的是 FTPS 还是 SFTP?

它们是不同的协议,您的主机可能认为您说的是错误的协议。

【讨论】:

  • 感谢您的回复。我与托管公司合作,问题得到了解决。原来是他们环境的一些设置。
  • ^ 您的托管环境会是 EC2 亚马逊云托管吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
  • 2016-07-05
相关资源
最近更新 更多