在C#/Mono中通过TLS/SSL通过Ftp上传

在C#/Mono中通过TLS/SSL通过Ftp上传,c#,ssl,ftp,mono,C#,Ssl,Ftp,Mono,目前,我正在使用以下代码尝试通过TLS/SSL进行Ftp(上传文件)。对于我正在进行的项目,我必须/被迫检查TLS/SSL以及用户名和密码组合。我也只能使用C#/Mono代码,所以大多数第三方解决方案都是行不通的 // Setting up variables FtpWebRequest ftpRequest; FtpWebResponse ftpResponse = null; // Configuring & creating the object ftpRequest = (Ft

目前,我正在使用以下代码尝试通过TLS/SSL进行Ftp(上传文件)。对于我正在进行的项目,我必须/被迫检查TLS/SSL以及用户名和密码组合。我也只能使用C#/Mono代码,所以大多数第三方解决方案都是行不通的

// Setting up variables
FtpWebRequest ftpRequest;
FtpWebResponse ftpResponse = null;

// Configuring & creating the object
ftpRequest = (FtpWebRequest)FtpWebRequest.Create (ftpPath + "/" + remoteFile);
ftpRequest.Credentials = new NetworkCredential ("user", "pwd");
ftpRequest.EnableSsl = true;
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = false;
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;

// Hooking up custom validation for certificates (currently this custom method returns "true" in all cases - so this is not a failure point to my knowledge)
ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
// Getting back the ServicePoint object
ServicePoint sp = ftpRequest.ServicePoint;

// Uploading logic
byte[] b = File.ReadAllBytes (localFile);
ftpRequest.ContentLength = b.Length;
using (Stream ftpStream = ftpRequest.GetRequestStream()) 
{
    ftpStream.Write (b, 0, b.Length);
    ftpStream.Close ();
    ftpResponse = (FtpWebResponse)ftpRequest.GetResponse ();
}
代码(没有TLS/SSL)工作得很好(当然,EnableSsl=false,我们在关闭TLS/SSL的情况下通过FTP服务器进行测试)。在TLS/SSL中添加时,我们使用(Stream-ftpStream=ftpRequest.GetRequestStream())永远不会超过行
。将引发以下错误:

System.Net.WebException: Kan geen verbinding met de externe server maken (translated: Unable to connect to the remote server)   
  at System.Net.FtpWebRequest.CheckError()   
  at System.Net.FtpWebRequest.GetRequestStream()   
  at ProjectNameSpace.FtpLogic.FtpUploadFileMethod()
我做错什么了吗?还有其他解决办法吗

编辑#1:我确实得到了包含证书信息等的ServicePoint对象。。代码还流畅地通过ServerCertificateValidationCallback方法,在所有情况下我都返回“true”


编辑#2:我应该通过FileZilla客户端添加,并且在FileZilla客户端中的TLS/SSL安全连接设置为on时,我可以使用给定的用户/pwd组合连接到服务器。所以我想情况并非如此

谢谢,
-Y有同样的问题,偶然发现了这个问题。在阅读了Andrew Savinykh的评论之后,我只是简单地添加了端口,它就工作了。希望这能帮助其他遇到这种情况的人

举例说明:

ftpRequest = (FtpWebRequest)FtpWebRequest.Create (ftpPath + ":990" + "/" + remoteFile);

该错误表示无法连接到远程服务器。很可能是因为它没有在听。尝试从您的计算机远程登录到目标计算机上的ssl端口,您认为remote应该监听该端口以证明/反驳。我应该通过FileZilla客户端添加这一点,并且在FileZilla客户端中启用TLS/ssl安全连接的设置后,我可以使用给定的用户/pwd组合连接到服务器。对不起,我想说的是有些服务有不同的SSL连接端口。显然不是你的情况。