可以使用FileZilla或WinSCP连接到FTP,但不能使用FtpWebRequest或FluentFTP



我正试图在代码中通过SSL打开FTP连接。我可以使用WinSCP的FileZilla连接并列出目录。但是当通过列出目录时。NET代码使用FtpWebClient,我得到错误

(425(无法打开数据连接

由于我可以从同一台计算机使用FileZilla进行连接,我不知道如何进行故障排除。

这是我的代码

public void FtpStuff()
{
string url = "ftp://my.server.com";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.Credentials = new NetworkCredential("myname", "password");
request.EnableSsl = true;
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
// This is the line that throws the exception
string line = streamReader.ReadLine();
}

我也试过FluentFTP。这是我的密码。我得到了异常

无法建立数据连接:不允许操作。

public void FtpStuff()
{
FtpClient client = new FtpClient();
client.Host = "my.server.com";
client.Credentials = new NetworkCredential("myname", "password");
client.EncryptionMode = FtpEncryptionMode.Explicit;
client.Connect();
// This line gives me an exception.
var files = client.GetListing();
}

以下是FluentFTP的日志信息。我更改了真实的用户名和IP,但其余的数据(包括端口(都是真实的数据。我的FTP服务提供商指定我必须在端口21上进行连接。问题似乎发生在EPSV命令发出并在新端口上建立连接之后的末尾。

# Connect()
The thread 0x5514 has exited with code 0 (0x0).
The thread 0xc80 has exited with code 0 (0x0).
The thread 0x89d4 has exited with code 0 (0x0).
Status:   Connecting to 123.123.123.123:21
Response: 220 FTP Server Ready
Command:  AUTH TLS
Response: 234 AUTH TLS successful
Status:   FTPS Authentication Successful
Status:   Time to activate encryption: 0h 0m 0s.  Total Seconds: 0.1339995.
Command:  USER me@mysite.com
The thread 0x6ddc has exited with code 0 (0x0).
Response: 331 Password required for me@mysite.com
Status:   Testing connectivity using Socket.Poll()...
Command:  PASS ***
Response: 230-***************************************************************************
Response: NOTICE TO USERS
Response: This computer system is private property. It is for authorized use only.
Response: Users (authorized or unauthorized) have no explicit or implicit
Response: expectation of privacy.
Response: 
Response: Any or all uses of this system and all files on this system may be
Response: intercepted, monitored, recorded, copied, audited and inspected by
Response: using this system, the user consents to such interception, monitoring,
Response: recording, copying, auditing, inspection, and disclosure at the
Response: discretion of such personnel or officials.  Unauthorized or improper use
Response: of this system may result in civil and criminal penalties and
Response: administrative or disciplinary action, as appropriate. By continuing to
Response: use this system you indicate your awareness of and consent to these terms
Response: and conditions of use. LOG OFF IMMEDIATELY if you do not agree to the
Response: conditions stated in this warning.
Response: ****************************************************************************
Response: 230 User me@mysite.com logged in
Command:  PBSZ 0
Response: 200 PBSZ 0 successful
Command:  PROT P
Response: 200 Protection set to Private
Command:  FEAT
Response: 211-Features:
Response: AUTH TLS
Response: CCC
Response: CLNT
Response: EPRT
Response: EPSV
Response: HOST
Response: MDTM
Response: MFF modify;UNIX.group;UNIX.mode;
Response: MFMT
Response: MLST modify*;perm*;size*;type*;unique*;UNIX.group*;UNIX.groupname*;UNIX.mode*;UNIX.owner*;UNIX.ownername*;
Response: PBSZ
Response: PROT
Response: REST STREAM
Response: SIZE
Response: SSCN
Response: TVFS
Response: 211 End
Status:   Text encoding: System.Text.ASCIIEncoding
Command:  SYST
Response: 215 UNIX Type: L8
# GetListing(null, Auto)
# GetWorkingDirectory()
Command:  PWD
Response: 257 "/" is the current directory
Command:  TYPE I
Response: 200 Type set to I
# OpenPassiveDataStream(AutoPassive, "MLSD /", 0)
Command:  EPSV
Response: 229 Entering Extended Passive Mode (|||50304|)
Status:   Connecting to 123.123.123.123:50304
Command:  MLSD /
Response: 150 Opening BINARY mode data connection for MLSD
Status:   FTPS Authentication Successful
Status:   Time to activate encryption: 0h 0m 0s.  Total Seconds: 0.1210002.
+---------------------------------------+
-----------------------------------------
Status:   Disposing FtpSocketStream...
# CloseDataStream()
Response: 425 Unable to build data connection: Operation not permitted
Status:   Disposing FtpSocketStream...
Exception thrown: 'FluentFTP.FtpCommandException' in FluentFTP.dll

这是我的FileZilla日志。

Status: Resolving address of mysite.com
Status: Connecting to 123.123.123.123:21...
Status: Connection established, waiting for welcome message...
Response:   220 FTP Server Ready
Command:    AUTH TLS
Response:   234 AUTH TLS successful
Status: Initializing TLS...
Status: Verifying certificate...
Status: TLS connection established.
Command:    USER me@mysite.com
Response:   331 Password required for me@mysite.com
Command:    PASS ************
Response:   230-***************************************************************************
Response:                                NOTICE TO USERS
Response:    This computer system is private property. It is for authorized use only.
Response:    Users (authorized or unauthorized) have no explicit or implicit
Response:    expectation of privacy.
Response:    
Response:    Any or all uses of this system and all files on this system may be
Response:    intercepted, monitored, recorded, copied, audited and inspected by
Response:    using this system, the user consents to such interception, monitoring,
Response:    recording, copying, auditing, inspection, and disclosure at the
Response:    discretion of such personnel or officials.  Unauthorized or improper use
Response:    of this system may result in civil and criminal penalties and
Response:    administrative or disciplinary action, as appropriate. By continuing to
Response:    use this system you indicate your awareness of and consent to these terms
Response:    and conditions of use. LOG OFF IMMEDIATELY if you do not agree to the
Response:    conditions stated in this warning.
Response:    ****************************************************************************
Response:   230 User me@mysite.com logged in
Command:    SYST
Response:   215 UNIX Type: L8
Command:    FEAT
Response:   211-Features:
Response:    AUTH TLS
Response:    CCC
Response:    CLNT
Response:    EPRT
Response:    EPSV
Response:    HOST
Response:    MDTM
Response:    MFF modify;UNIX.group;UNIX.mode;
Response:    MFMT
Response:    MLST modify*;perm*;size*;type*;unique*;UNIX.group*;UNIX.groupname*;UNIX.mode*;UNIX.owner*;UNIX.ownername*;
Response:    PBSZ
Response:    PROT
Response:    REST STREAM
Response:    SIZE
Response:    SSCN
Response:    TVFS
Response:   211 End
Status: Server does not support non-ASCII characters.
Command:    PBSZ 0
Response:   200 PBSZ 0 successful
Command:    PROT P
Response:   200 Protection set to Private
Status: Logged in
Status: Retrieving directory listing...
Command:    PWD
Response:   257 "/" is the current directory
Command:    TYPE I
Response:   200 Type set to I
Command:    PASV
Response:   227 Entering Passive Mode (123,123,123,123,197,68).
Command:    MLSD
Response:   150 Opening BINARY mode data connection for MLSD
Response:   226 Transfer complete
Status: Directory listing of "/" successful

我也可以使用WinSCP进行连接。正如评论中所建议的,我确实在打开数据连接时检查了TLS/SSL会话ID是否被重用。似乎是。

227 Entering Passive Mode (???)
MLSD
Connecting to ??? ...
Connection pending
Data connection opened
Trying reuse main TLS session ID
Session ID reused
150 Opening data channel for directory listing of "/"

。NET框架不支持TLS/SSL会话重用。如果您的服务器需要它(它看起来是什么,它做什么,现在很常见,什么对安全性有好处(,则不能使用FtpWebRequest或FluentFTP。两者都使用。NET实现TLS/SSL。

您必须使用FTP库,该库使用自己的TLS/SSL实现。

您可以使用myWinSCP。NET程序集尽管与FluentFTP相反,它不是本地的。NET库,它依赖于一个外部二进制文件。但这正是它发挥作用的原因

一些参考文献:

  • https://github.com/robinrodricks/FluentFTP/issues/347
  • https://github.com/dotnet/runtime/issues/27916
  • "由于远程方已关闭传输流",认证失败;使用FluentFTP通过TLS/SSL与FTP服务器进行传输时
  • 使用TLS会话重用将文件上载到C#中的隐式FTPS服务器
  • 突然间;150打开用于从服务器下载文件的数据通道";在FTP下载运行多年之后——根据这篇文章和其他地方的参考资料,TLS/SSL会话重用在早期得到了支持。NET Framework,但某些更新破坏了它。NET核心,但它从未工作过(另请参阅上面的dotnetGitHub链接(

相关内容

最新更新