我正在使用一个C#控制台应用程序来重现这个问题。这是一个.NET Framework4.7应用程序。FluentFTP版本为24.0.0,作为Nuget包安装。
这是我的密码:
using FluentFTP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace FTP_Test
{
class Program
{
private static string host = "<my host>";
private static string username = "<my username>";
private static string pass = "<my pass>";
private static int port = 990;
static void Main(string[] args)
{
FtpClient ftpClient = new FtpClient(host, port, username, pass);
ftpClient.DataConnectionType = FtpDataConnectionType.EPSV;
ftpClient.EncryptionMode = FtpEncryptionMode.Implicit;
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
ftpClient.DataConnectionEncryption = true;
ftpClient.ValidateCertificate += new FtpSslValidation(OnValidateCertificate);
var cer = new System.Security.Cryptography.X509Certificates.X509Certificate2();
ftpClient.ClientCertificates.Add(cer);
ServicePointManager.ServerCertificateValidationCallback = ServerCertificateValidationCallback;
ftpClient.Connect();
}
private static void OnValidateCertificate(FtpClient control, FtpSslValidationEventArgs e)
{
// add logic to test if certificate is valid here
e.Accept = true;
}
private static bool ServerCertificateValidationCallback(object sender,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
}
}
我在控制台中得到的错误(以及堆栈跟踪):
Unhandled Exception: System.IO.IOException: Authentication failed because the remote party has closed the transport stream.
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation)
at FluentFTP.FtpSocketStream.ActivateEncryption(String targethost, X509CertificateCollection clientCerts, SslProtocols sslProtocols)
at FluentFTP.FtpClient.Connect()
at FTP_Test.Program.Main(String[] args) in C:\Users\Nemanja\source\repos\FTP Test\Program.cs:line 35
我在这里错过了什么?
我可以使用相同的主机/用户/pass通过Filezilla进行连接。在Filezilla中,我必须设置以下内容:
当我在调试模式下运行应用程序并在函数OnValidateCertificate和ServerCertificateValidationCallback中设置断点时,这些断点不会被击中。
发布于 2022-02-02 11:37:45
原来FluentFTP不支持隐式SSL,所以我不得不切换到Chilkat。
示例-code.com为这个库提供了很好的示例,下面是指向隐式SSL:https://www.example-code.com/csharp/ftp_implicitSSL.asp示例的链接
在这里复制示例(从上面的链接),以防链接停止工作。
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Chilkat.Ftp2 ftp = new Chilkat.Ftp2();
// If this example does not work, try using passive mode
// by setting this to true.
ftp.Passive = false;
ftp.Hostname = "ftp.something.com";
ftp.Username = "test";
ftp.Password = "test";
ftp.Port = 990;
// We don't want AUTH SSL:
ftp.AuthTls = false;
// We want Implicit SSL:
ftp.Ssl = true;
// Connect and login to the FTP server.
bool success = ftp.Connect();
if (success != true) {
Debug.WriteLine(ftp.LastErrorText);
return;
}
else {
// LastErrorText contains information even when
// successful. This allows you to visually verify
// that the secure connection actually occurred.
Debug.WriteLine(ftp.LastErrorText);
}
Debug.WriteLine("FTPS Channel Established!");
// Do whatever you're doing to do ...
// upload files, download files, etc...
success = ftp.Disconnect();
发布于 2022-06-28 18:13:40
如果您使用的是.NETFramework 4.5,则FluentFTP支持最高可达Tls1.2的隐式Tls。本评论时的FluentFTP不支持高于4.5的.NETFramework。这对我来说很管用:
Uri uri = new Uri(FTP_URL);
using (var conn = new FtpClient(uri.Host, FTP_Username, FTP_Password))
{
conn.EncryptionMode = FtpEncryptionMode.Implicit;
conn.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
conn.ValidateAnyCertificate = true;
conn.Connect();
conn.UploadFile(file, FileName, FtpRemoteExists.Overwrite, false, FtpVerify.Retry);
conn.Disconnect();
}
https://stackoverflow.com/questions/70827447
复制相似问题