Upload file to implicit FTPS server in C# with TLS

2020-07-30 00:14发布

问题:

I'm trying to upload file to FileZilla server through ftps by protocol TLS. On the server port 20 and 21 is closed. The only way how I managed to connect to server is by using FluentFTP but I couldn't upload file because of some FileZilla server bug.

https://github.com/robinrodricks/FluentFTP/issues/335
https://forum.filezilla-project.org/viewtopic.php?t=51601

public static void UploadTest(
    string pathUploadFile, string addressIP, int port, string location,
    string userName, string password)
{
    FtpClient ftp;

    Console.WriteLine("Configuring FTP to Connect to {0}", addressIP);
    ftp = new FtpClient(addressIP, port, new NetworkCredential(userName, password));
    ftp.ConnectTimeout = 600000;
    ftp.ReadTimeout = 60000;
    ftp.EncryptionMode = FtpEncryptionMode.Implicit;
    ftp.SslProtocols = SslProtocols.Default | SslProtocols.Tls11 | SslProtocols.Tls12;
    ftp.ValidateCertificate += new FtpSslValidation(OnValidateCertificate);
    ftp.Connect();
    // upload a file
    ftp.UploadFile(pathUploadFile, location);

    Console.WriteLine("Connected to {0}", addressIP);
    ftp.Disconnect();

    void OnValidateCertificate(FtpClient control, FtpSslValidationEventArgs e)
    {
        // add logic to test if certificate is valid here
        e.Accept = true;
    }
}

Is there any way around without a violating security level? If not is there any other free library which support uploading files with TLS/SSL? I also tried this but it didn't work.
https://docs.microsoft.com/en-us/dotnet/api/system.net.ftpwebrequest.enablessl

Thanks.

回答1:

If you are using .NET Framework (not .NET Core), you can use WinSCP .NET assembly.

It supports implicit TLS (port 990). And uses OpenSSL TLS implementation (not .NET Framework), so it should not have the problem that FluentFTP has. It definitely works for me against FileZilla FTP server, even with session resumption requirement turned on.

SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "ftp.example.com",
    UserName = "username",
    Password = "password",
    FtpSecure = FtpSecure.Implicit,
    TlsHostCertificateFingerprint = "xx:xx:xx:...",
};

using (Session session = new Session())
{
    session.Open(sessionOptions);

    session.PutFiles(localPath, remotePath).Check();
}

(I'm the author of WinSCP)



标签: c# .net ssl ftp ftps