Reputation: 3473
Currently I'm doing using the following code to attempt Ftp (uploading a file) over TLS/SSL. For the project I'm on I'm obliged/constrained to go over TLS/SSL w/ username&password combination. I'm also contrained to only C#/Mono code, so most thrid party solutions are no go.
// 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 ();
}
The code (without TLS/SSL) works perfectly (ofcourse .EnableSsl = false, and we were testing via an FTP server with TLS/SSL turned off). When adding in TLS/SSL we never reach past the line using (Stream ftpStream = ftpRequest.GetRequestStream())
. The following error is thrown:
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()
Is there something I'm doing wrong, are there other solutions?
EDIT #1: I do get back the ServicePoint object that contains the certificate info etc.. The code also flows through the ServerCertificateValidationCallback method fluently, where I return "true" in all cases.
EDIT #2: I should add that via FileZilla Client AND with the settings on for TLS/SSL secure connections in FileZilla client I can connect with given user/pwd combination to the server. So that's not the case I guess.
Thanks, -Y
Upvotes: 0
Views: 1733
Reputation: 1
Had the same issue and stumbled upon this question. After reading Andrew Savinykh's comment, I've simply added the port and it worked. Hope this helps others who come across this.
For illustration:
ftpRequest = (FtpWebRequest)FtpWebRequest.Create (ftpPath + ":990" + "/" + remoteFile);
Upvotes: 0