c# - 在 TLS : The remote server return an error : 234 AUTH TLS OK 上使用 FtpWebRequest 的问题

标签 c# .net ssl ftp ftpwebrequest

我必须使用 C# 应用程序 (.NET 3.5) 在使用 TLS 的 FTP 上上传一些文件。使用 FileZilla,没问题。

现在,在我的 C# 代码中,我得到了这个异常:

The remote server return an error : 234 AUTH TLS OK.

我真的不知道为什么,因为 FileZilla 一切正常。

这是我的代码:

public static bool AcceptAllCertificatePolicy(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    return true;
}

public static string Upload_SSL(string filenameSrc)
{

    ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;

    FileInfo fileInfSrc = new FileInfo(filenameSrc);
    FtpWebRequest reqFTP;

    // Create FtpWebRequest object from the Uri provided
    if (strDirectory.Trim() != "")
    {
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strHost.Trim() + "/" + strDirectory.Trim() + "/" + fileInfSrc.Name.Trim()));
    }
    else
    {
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strHost.Trim() + "/" + fileInfSrc.Name.Trim()));
    }

    // Provide the WebPermission Credintials
    reqFTP.Credentials = new NetworkCredential(strUser.Trim(), strPass.Trim());

    reqFTP.EnableSsl = true;

    // Test Fabio du 15/01/2013
    reqFTP.Proxy = null;

    // By default KeepAlive is true, where the control connection is not closed
    // after a command is executed.
    reqFTP.KeepAlive = false;

    // Specify the command to be executed.
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

    // Specify the data transfer type.
    reqFTP.UseBinary = true;

    // Notify the server about the size of the uploaded file
    reqFTP.ContentLength = fileInfSrc.Length;

    // The buffer size is set to 8kb
    int buffLength = 8192;
    byte[] buff = new byte[buffLength];
    int contentLen;

    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
    FileStream fs = fileInfSrc.OpenRead();

    try
    {
        // Stream to which the file to be upload is written
        Stream strm = reqFTP.GetRequestStream();

        // Read from the file stream 2kb at a time
        contentLen = fs.Read(buff, 0, buffLength);


        // Till Stream content ends
        while (contentLen != 0)
        {
            // Write Content from the file stream to the FTP Upload Stream
            strm.Write(buff, 0, contentLen);
            contentLen = fs.Read(buff, 0, buffLength);
        }

        // Close the file stream and the Request Stream
        strm.Close();
        fs.Close();
    }
    catch (Exception ex)
    {
        fs.Close();
        return (ex.Message);
    }

    return "ok";
}

这是我的电话:

myFtp.Class1.strHost = "ftp://XXXXXXXXXXXXX";
myFtp.Class1.strPass = "*****************";
myFtp.Class1.strUser = "*********";
myFtp.Class1.nPort = 21;
myFtp.Class1.Upload_SSL(@"D:\Test.txt");

这是我的应用程序日志:

System.Net Information: 0 : [35744] FtpWebRequest#44624228::.ctor(ftp://XXXXXXXXXX/Test.txt)
System.Net Information: 0 : [35744] FtpWebRequest#44624228::GetRequestStream(Méthode=STOR.)
System.Net Information: 0 : [35744] Associating FtpWebRequest#44624228 with FtpControlStream#17654054
System.Net Information: 0 : [35744] FtpControlStream#17654054 - Réponse reçue [220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 50 allowed.
220-Local time is now 17:14. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.]
System.Net Information: 0 : [35744] FtpControlStream#17654054 - Envoi de la commande [AUTH TLS]
System.Net Information: 0 : [35744] FtpControlStream#17654054 - Réponse reçue [234 AUTH TLS OK.]
System.Net Information: 0 : [35744] TlsStream#52727599::.ctor(host=XXXXXX.shop, #certs=0)
System.Net Information: 0 : [35744] FtpControlStream#17654054 - Envoi de la commande [PBSZ 0]
System.Net Information: 0 : [35744] SecureChannel#14347911::.ctor(hostname=XXXXXX.shop, #clientCertificates=0)
System.Net Information: 0 : [35744] Énumération des packages de sécurité :
System.Net Information: 0 : [35744]     Negotiate
System.Net Information: 0 : [35744]     NegoExtender
System.Net Information: 0 : [35744]     Kerberos
System.Net Information: 0 : [35744]     NTLM
System.Net Information: 0 : [35744]     Schannel
System.Net Information: 0 : [35744]     Microsoft Unified Security Protocol Provider
System.Net Information: 0 : [35744]     WDigest
System.Net Information: 0 : [35744]     TSSSP
System.Net Information: 0 : [35744]     pku2u
System.Net Information: 0 : [35744]     CREDSSP
System.Net Information: 0 : [35744] SecureChannel#14347911 - Laissé avec un choix de 0 certificats clients.
System.Net Information: 0 : [35744] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent  = Outbound, scc     = System.Net.SecureCredential)
System.Net Information: 0 : [35744] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = (null), targetName = XXXXXXXX, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [35744] InitializeSecurityContext(longueur de In-Buffer=0, longueur de Out-Buffer=127, code retourné=ContinueNeeded).
System.Net Information: 0 : [35744] FtpWebRequest#44624228::(Libération de la connexion FTP n° 17654054.)
System.Net Information: 0 : [35744] Associating FtpWebRequest#44624228 with FtpControlStream#51393439
System.Net Information: 0 : [35744] FtpControlStream#51393439 - Réponse reçue [220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 50 allowed.
220-Local time is now 17:14. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.]
System.Net Information: 0 : [35744] FtpControlStream#51393439 - Envoi de la commande [AUTH TLS]
System.Net Information: 0 : [35744] FtpControlStream#51393439 - Réponse reçue [234 AUTH TLS OK.]
System.Net Information: 0 : [35744] TlsStream#26756241::.ctor(host=XXXXXXXXXXX, #certs=0)
System.Net Information: 0 : [35744] FtpControlStream#51393439 - Envoi de la commande [PBSZ 0]
System.Net Information: 0 : [35744] SecureChannel#23264094::.ctor(hostname=XXXXXXXXXXXX, #clientCertificates=0)
System.Net Information: 0 : [35744] SecureChannel#23264094 - Laissé avec un choix de 0 certificats clients.
System.Net Information: 0 : [35744] Utilisation du gestionnaire d'informations d'identification mises en cache.
System.Net Information: 0 : [35744] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = (null), targetName = XXXXXXXXXXX, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [35744] InitializeSecurityContext(longueur de In-Buffer=0, longueur de Out-Buffer=127, code retourné=ContinueNeeded).
System.Net Information: 0 : [35744] FtpWebRequest#44624228::(Libération de la connexion FTP n° 51393439.)
System.Net Error: 0 : [35744] Exception in the FtpWebRequest#44624228::GetRequestStream - Le serveur distant a retourné une erreur : 234 AUTH TLS OK.
.
System.Net Error: 0 : [35744]    à System.Net.FtpWebRequest.CheckError()
   à System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   à System.Net.CommandStream.Abort(Exception e)
   à System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
   à System.Net.FtpWebRequest.GetRequestStream()

这是我的 FileZilla 日志:

Statut :    Résolution de l'adresse de XXXXXXXXXXX
Statut :    Connexion à XX.XX.XX.XX:21...
Statut :    Connexion établie, attente du message d'accueil...
Suivi : CFtpControlSocket::OnReceive()
Réponse :   220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
Réponse :   220-You are user number 1 of 50 allowed.
Réponse :   220-Local time is now 17:18. Server port: 21.
Réponse :   220-This is a private system - No anonymous login
Réponse :   220-IPv6 connections are also welcome on this server.
Réponse :   220 You will be disconnected after 15 minutes of inactivity.
Suivi : CFtpControlSocket::SendNextCommand()
Commande :  AUTH TLS
Suivi : CFtpControlSocket::OnReceive()
Réponse :   234 AUTH TLS OK.
Statut :    Initialisation de TLS...
Suivi : CTlsSocket::Handshake()
Suivi : CTlsSocket::ContinueHandshake()
Suivi : TLS handshake: About to send CLIENT HELLO
Suivi : TLS handshake: Sent CLIENT HELLO
Suivi : CTlsSocket::OnSend()
Suivi : CTlsSocket::OnRead()
Suivi : CTlsSocket::ContinueHandshake()
Suivi : CTlsSocket::OnRead()
Suivi : CTlsSocket::ContinueHandshake()
Suivi : TLS handshake: Received SERVER HELLO
Suivi : TLS handshake: Processed SERVER HELLO
Suivi : CTlsSocket::OnRead()
Suivi : CTlsSocket::ContinueHandshake()
Suivi : TLS handshake: Received CERTIFICATE
Suivi : TLS handshake: Processed CERTIFICATE
Suivi : TLS handshake: Received SERVER HELLO DONE
Suivi : TLS handshake: Processed SERVER HELLO DONE
Suivi : TLS handshake: About to send CLIENT KEY EXCHANGE
Suivi : TLS handshake: Sent CLIENT KEY EXCHANGE
Suivi : TLS handshake: About to send FINISHED
Suivi : TLS handshake: Sent FINISHED
Suivi : CTlsSocket::OnRead()
Suivi : CTlsSocket::ContinueHandshake()
Suivi : TLS handshake: Received NEW SESSION TICKET
Suivi : TLS handshake: Processed NEW SESSION TICKET
Suivi : TLS handshake: Received FINISHED
Suivi : TLS handshake: Processed FINISHED
Suivi : TLS Handshake successful
Suivi : Protocol: TLS1.2, Key exchange: RSA, Cipher: AES-256-GCM, MAC: AEAD
Statut :    Vérification du certificat...
Statut :    Connexion TLS établie.
Suivi : CFtpControlSocket::SendNextCommand()
Commande :  USER XXXXXXXXXXXXXXXX
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   331 User XXXXXXXXXXXXXXXXXX OK. Password required
Suivi : CFtpControlSocket::SendNextCommand()
Commande :  PASS ****************************
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   230 OK. Current directory is /
Suivi : CFtpControlSocket::SendNextCommand()
Commande :  OPTS UTF8 ON
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   200 OK, UTF-8 enabled
Suivi : CFtpControlSocket::SendNextCommand()
Commande :  PBSZ 0
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   200 PBSZ=0
Suivi : CFtpControlSocket::SendNextCommand()
Commande :  PROT P
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   200 Data protection level set to "private"
Statut :    Connecté
Suivi : CFtpControlSocket::ResetOperation(0)
Suivi : CControlSocket::ResetOperation(0)
Suivi : CFileZillaEnginePrivate::ResetOperation(0)
Suivi : Measured latency of 15 ms
Suivi : CFtpControlSocket::FileTransfer()
Statut :    Démarrage de l'envoi de C:\Users\XXXXXX\Downloads\desktop.ini
Suivi : CFtpControlSocket::SendNextCommand()
Suivi : CFtpControlSocket::ChangeDirSend()
Commande :  CWD /
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   250 OK. Current directory is /
Suivi : CFtpControlSocket::ResetOperation(0)
Suivi : CControlSocket::ResetOperation(0)
Suivi : CFtpControlSocket::ParseSubcommandResult(0)
Suivi : FileTransferSubcommandResult()
Suivi : CFtpControlSocket::SendNextCommand()
Suivi : FileTransferSend()
Suivi : CFtpControlSocket::SendNextCommand()
Suivi : CFtpControlSocket::TransferSend()
Suivi :   state = 1
Commande :  TYPE I
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   200 TYPE is now 8-bit binary
Suivi : CFtpControlSocket::TransferParseResponse()
Suivi :   code = 2
Suivi :   state = 1
Suivi : CFtpControlSocket::SendNextCommand()
Suivi : CFtpControlSocket::TransferSend()
Suivi :   state = 2
Commande :  PASV
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   227 Entering Passive Mode (188,165,33,200,32,110)
Suivi : CFtpControlSocket::TransferParseResponse()
Suivi :   code = 2
Suivi :   state = 2
Suivi : CFtpControlSocket::SendNextCommand()
Suivi : CFtpControlSocket::TransferSend()
Suivi :   state = 4
Suivi : Binding data connection source IP to control connection source IP 172.16.0.10
Commande :  STOR desktop.ini
Suivi : CTransferSocket::OnConnect
Suivi : CTlsSocket::Handshake()
Suivi : Trying to resume existing TLS session.
Suivi : CTlsSocket::ContinueHandshake()
Suivi : TLS handshake: About to send CLIENT HELLO
Suivi : TLS handshake: Sent CLIENT HELLO
Suivi : CTlsSocket::OnSend()
Suivi : CTlsSocket::OnSend()
Suivi : CTlsSocket::OnRead()
Suivi : CTlsSocket::ContinueHandshake()
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   150 Accepted data connection
Suivi : CFtpControlSocket::TransferParseResponse()
Suivi :   code = 1
Suivi :   state = 4
Suivi : CFtpControlSocket::SendNextCommand()
Suivi : CFtpControlSocket::TransferSend()
Suivi :   state = 5
Suivi : CTlsSocket::OnRead()
Suivi : CTlsSocket::ContinueHandshake()
Suivi : TLS handshake: Received SERVER HELLO
Suivi : TLS handshake: Processed SERVER HELLO
Suivi : TLS handshake: Received FINISHED
Suivi : TLS handshake: Processed FINISHED
Suivi : TLS handshake: About to send FINISHED
Suivi : TLS handshake: Sent FINISHED
Suivi : TLS Handshake successful
Suivi : TLS Session resumed
Suivi : Protocol: TLS1.2, Key exchange: RSA, Cipher: AES-256-GCM, MAC: AEAD
Suivi : CTransferSocket::OnConnect
Suivi : CTlsSocket::Shutdown()
Suivi : CTransferSocket::TransferEnd(1)
Suivi : CFtpControlSocket::TransferEnd()
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   226-File successfully transferred
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   226 0.004 seconds (measured here), 67.83 Kbytes per second
Suivi : CFtpControlSocket::TransferParseResponse()
Suivi :   code = 2
Suivi :   state = 7
Suivi : CFtpControlSocket::ResetOperation(0)
Suivi : CControlSocket::ResetOperation(0)
Suivi : CFtpControlSocket::ParseSubcommandResult(0)
Suivi : FileTransferSubcommandResult()
Suivi : CFtpControlSocket::ResetOperation(0)
Suivi : CControlSocket::ResetOperation(0)
Statut :    Transfert de fichier réussi, 282 octets transférés en 1 seconde
Suivi : CFileZillaEnginePrivate::ResetOperation(0)
Statut :    Récupération du contenu du dossier "/"...
Suivi : CFtpControlSocket::ParseSubcommandResult(0)
Suivi : CFtpControlSocket::ListSubcommandResult()
Suivi :   state = 1
Suivi : CFtpControlSocket::SendNextCommand()
Suivi : CFtpControlSocket::TransferSend()
Suivi :   state = 2
Commande :  PASV
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   227 Entering Passive Mode (188,165,33,200,128,109)
Suivi : CFtpControlSocket::TransferParseResponse()
Suivi :   code = 2
Suivi :   state = 2
Suivi : CFtpControlSocket::SendNextCommand()
Suivi : CFtpControlSocket::TransferSend()
Suivi :   state = 4
Suivi : Binding data connection source IP to control connection source IP 172.16.0.10
Commande :  MLSD
Suivi : CTransferSocket::OnConnect
Suivi : CTlsSocket::Handshake()
Suivi : Trying to resume existing TLS session.
Suivi : CTlsSocket::ContinueHandshake()
Suivi : TLS handshake: About to send CLIENT HELLO
Suivi : TLS handshake: Sent CLIENT HELLO
Suivi : CTlsSocket::OnSend()
Suivi : CTlsSocket::OnSend()
Suivi : CTlsSocket::OnRead()
Suivi : CTlsSocket::ContinueHandshake()
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   150 Accepted data connection
Suivi : CFtpControlSocket::TransferParseResponse()
Suivi :   code = 1
Suivi :   state = 4
Suivi : CFtpControlSocket::SendNextCommand()
Suivi : CFtpControlSocket::TransferSend()
Suivi :   state = 5
Suivi : CTlsSocket::OnRead()
Suivi : CTlsSocket::ContinueHandshake()
Suivi : TLS handshake: Received SERVER HELLO
Suivi : TLS handshake: Processed SERVER HELLO
Suivi : TLS handshake: Received FINISHED
Suivi : TLS handshake: Processed FINISHED
Suivi : TLS handshake: About to send FINISHED
Suivi : TLS handshake: Sent FINISHED
Suivi : TLS Handshake successful
Suivi : TLS Session resumed
Suivi : Protocol: TLS1.2, Key exchange: RSA, Cipher: AES-256-GCM, MAC: AEAD
Suivi : CTransferSocket::OnConnect
Suivi : CTlsSocket::OnRead()
Suivi : CFtpControlSocket::OnReceive()
Réponse :   226-Options: -a -l 
Réponse :   226 4 matches total
Suivi : CFtpControlSocket::TransferParseResponse()
Suivi :   code = 2
Suivi :   state = 5
Suivi : CFtpControlSocket::SendNextCommand()
Suivi : CFtpControlSocket::TransferSend()
Suivi :   state = 8
Suivi : CTlsSocket::OnRead()
Suivi : CTransferSocket::OnReceive(), m_transferMode=0
Suivi : CTransferSocket::TransferEnd(1)
Suivi : CFtpControlSocket::TransferEnd()
Suivi : CFtpControlSocket::ResetOperation(0)
Suivi : CControlSocket::ResetOperation(0)
Suivi : CFtpControlSocket::ParseSubcommandResult(0)
Suivi : CFtpControlSocket::ListSubcommandResult()
Suivi :   state = 3
Suivi : CFtpControlSocket::ResetOperation(0)
Suivi : CControlSocket::ResetOperation(0)
Statut :    Contenu du dossier "/" affiché avec succès
Suivi : CFileZillaEnginePrivate::ResetOperation(0)

有人有什么想法吗?

编辑:这是服务器上的日志:

Nov 29 10:12:09 ns309686 pure-ftpd: (?@LNeuilly-656-1-52-66.w80-11.abo.wanadoo.fr) [INFO] New connection from LNeuilly-656-1-52-66.w80-11.abo.wanadoo.fr
Nov 29 10:12:09 ns309686 pure-ftpd: (?@LNeuilly-656-1-52-66.w80-11.abo.wanadoo.fr) [WARNING] Sorry, cleartext sessions are not accepted on this server.#012Please reconnect using SSL/TLS secu$
Nov 29 10:12:10 ns309686 pure-ftpd: (?@LNeuilly-656-1-52-66.w80-11.abo.wanadoo.fr) [INFO] New connection from LNeuilly-656-1-52-66.w80-11.abo.wanadoo.fr
Nov 29 10:12:10 ns309686 pure-ftpd: (?@LNeuilly-656-1-52-66.w80-11.abo.wanadoo.fr) [WARNING] Sorry, cleartext sessions are not accepted on this server.#012Please reconnect using SSL/TLS secu$

---> 似乎 TLS 未启用,但为什么?

非常感谢:)

最佳答案

我相信您的服务器需要 TLS 1.2。而且您使用的是 .NET 3.5,它不支持 TLS 1.2。

尝试切换到 .NET 4.5.2 或更新版本。

参见 https://learn.microsoft.com/en-us/dotnet/api/system.security.authentication.sslprotocols

关于c# - 在 TLS : The remote server return an error : 234 AUTH TLS OK 上使用 FtpWebRequest 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40849090/

相关文章:

c# - 如何生成一个范围内的加密安全随机整数?

c# - Azure辅助角色计划任务

c# - ASP MVC Less 文件给出 : Exception has been thrown by the target of an invocation

c# - 具有相同签名的成员已被声明

ssl - 使用带有指向 EC2 实例的 CNAME 的通配符 SSL

c# - Windows 应用商店应用程序 AppContainerCheck 失败

c# - .NET - neo4jclient - 从密码查询中检索结果

.net - 使用流读取文件时写入文件?

swift - Alamofire 不处理身份验证挑战

ruby - 如何将 TLS 客户端证书与 RubyGems 注册表一起使用?