Can connect to FTP using FileZilla or WinSCP, but not with FtpWebRequest or FluentFTP
.NET framework does not support TLS/SSL session reuse. If your server requires it (what it looks it does and what is quite common nowadays and what is good thing for security), you cannot use FtpWebRequest
nor FluentFTP. Both use the .NET implementation of TLS/SSL.
You will have to use FTP library that uses own TLS/SSL implementation.
You can use my WinSCP .NET assembly. Though contrary to FluentFTP, it's not a native .NET library, it has dependencies on an external binary. But that's what makes it working.
Some references:
- https://github.com/robinrodricks/FluentFTP/issues/347
- https://github.com/dotnet/runtime/issues/27916
- "Authentication failed because the remote party has closed the transport stream" when transferring to/from FTP server over TLS/SSL using FluentFTP
- Upload file to implicit FTPS server in C# with TLS session reuse
- Suddenly getting "150 Opening Data channel for file download from server" after the FTP downloads was working for years – According to this post and other references elsewhere, the TLS/SSL session reuse was supported earlier with .NET Framework, but some update broke it. In .NET Core it was never working (see also the
dotnet
GitHub link above).
Fluent FTP Won't connect to FTP Server
From the official GitHub page:
SFTP is not supported as it is FTP over SSH, a completely different protocol (use SSH.NET for that)
You're trying to use an FTP library to connect to an SFTP server. They're two completely different protocols that have nothing in common, despite fulfulling the same goal. Use a proper SFTP library for that server.
Both FileZilla and WinSCP support both FTP and SFTP, that's why you have no problems with them.
Upload file to implicit FTPS server in C# with TLS session reuse
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)
For more references about the problem, see also Can connect to FTP using FileZilla or WinSCP, but not with FtpWebRequest or FluentFTP.
Cannot login in with Fluent FTP (Error 530 – Not Logged In), but GUI FTP client works
You are using SFTP in FileZilla, not FTP.
So you have to use SFTP library in your code, not FTP library.
See How to communicate with SFTP server.
Connecting to FTPS (FTP over SSL) with FluentFTP
As you seem to be connecting to the default port 21 (no explicit port specified anywhere), you need to use the "Explicit" mode:
conn.EncryptionMode = FtpEncryptionMode.Explicit;
If the server uses a self-signed certificate, you may need to verify it programmatically. Do not blindly accept any certificate, as the answer by @Ivan does. That's a security flaw. Validate the specific certificate, e.g. by checking its fingerprint.
See FtpWebRequest "The remote certificate is invalid according to the validation procedure".
Related Topics
How to Check If a Class Inherits Another Class Without Instantiating It
Best Way to Check for Nullable Bool in a Condition Expression (If ...)
Cannot Resolve Scoped Service from Root Provider .Net Core 2
Invalid Cast from 'System.Int32' to 'System.Nullable'1[[System.Int32, Mscorlib]]
How to Access Session in a Webmethod
How to Click a Button in a Webbrowser Control
How to Get Awaitable Thread.Sleep
Error Message "Cs5001 Program Does Not Contain a Static 'Main' Method Suitable for an Entry Point"
How to Loop Over the Properties of a Class
Initializing an Array of Structs in C#
Detecting Client Death in Wcf Duplex Contracts
Simplest Way to Run Three Methods in Parallel in C#
Index of Currently Selected Row in Datagridview
Explicitly Freeing Memory in C#
Run the Current Application as Single Instance and Show the Previous Instance