2016-11-28 7 views
4

Мне нужно загрузить некоторые файлы на FTP, который использует TLS, с моим приложением C# (.NET 3.5). С FileZilla никаких проблем.Сбой в использовании FtpWebRequest на TLS: Удаленный сервер возвращает ошибку: 234 AUTH TLS OK

Теперь, с моей C# код, я получаю это исключение:

Удаленный сервер возвращает ошибку: 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) 

Кто-нибудь есть какие-то идеи, пожалуйста?

EDIT: Вот журнал на сервере:

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

---> Окажись TLS не включен, но почему?

Большое спасибо :)

+0

ли сервер общедоступным для тестирования? (никаких учетных данных не требуется, только IP-адрес) –

+0

Я не думаю, что проблема заключается в сервере. «234 ...» - это именно тот ответ, который ожидается сервером. См. [RFC 4217] (https://tools.ietf.org/html/rfc4217#page-17). –

+0

@ Steffen Конечно. Но я бы сказал, что сообщение вводит в заблуждение. Кажется, что FtpWebRequest не может инициализировать шифрование для некоторой (возможно, редкой) причины. И он использует последний ответ с сервера в недопустимой попытке объяснить ошибку. –

ответ

2

Я считаю, что ваш сервер требует TLS 1.2. И вы используете .NET 3.5, который не поддерживает TLS 1.2.

Попробуйте переключиться на .NET 4.5.2 или новее.

См https://msdn.microsoft.com/en-us/library/system.security.authentication.sslprotocols.aspx

+0

TLS 1.2 был добавлен в 4.5. 2, которая является самой ранней поддерживаемой версией. –

+0

@PanagiotisKanavos Спасибо. У вас есть ссылка на это? –

+0

[Поддержка завершения для .NET Framework 4, 4.5 и 4.5.1] (https://blogs.msdn.microsoft.com/dotnet/2015/12/09/support-ending-for-the-net-framework- 4-4-5-и-4-5-1 /) произошло почти год назад. Это было объявлено в августе 2014 года. Сейчас мы находимся на этапе, когда популярные библиотеки с открытым исходным кодом отбрасывают поддержку 4.0. –