C# FtpWebRequest与启用SSL

C# FtpWebRequest与启用SSL,c#,ssl,ftp,ftpwebrequest,C#,Ssl,Ftp,Ftpwebrequest,我实现了我的自定义FTP类,以便与我付费的托管服务器一起工作。 我使用FTP备份、恢复和更新我的应用程序。 我现在想启用ssl以将其投入生产。 我问我的托管公司是否支持ssl协议,他们说支持 因此,在Microsoft MSDN教程之后,我将我的方法修改为: reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(m_ftpAddress.Trim())); reqFTP.UseBinary = true; reqFTP.Credentials

我实现了我的自定义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进行询问时,它将不会引发异常。这可以在跟踪日志中看到。因此,我认为这不是服务器问题

在身份验证阶段之后,服务器返回

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

触发错误的消息:

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

我试着设置

reqFTP.UsePassive = true;
属性设置为false,然后我得到以下错误:

系统.Net信息:0:[2692]FtpControlStream#4878312-收到响应[500非法端口命令]

当然,如果EnableSLL属性没有设置为true,那么一切都可以正常工作

有人知道这件事吗

编辑:我将代码修改为fallows:

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软件有问题,代码也没有问题

您是否已检查主机是否使用FTPS或SFTP


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

谢谢您的回复。我和托管公司一起工作,问题解决了。这是他们的环境设置。^您的托管环境是否恰好是EC2亚马逊云托管?然后将您的编辑作为答案发布并接受。