在 TLS 上使用 FtpWebRequest 的问题:远程服务器返回错误:234 AUTH TLS OK

Wal*_*oni 4 .net c# ftp ssl ftpwebrequest

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

现在,使用我的 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";
}
Run Code Online (Sandbox Code Playgroud)

这是我的电话:

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

这是我的应用程序日志:

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";
}
Run Code Online (Sandbox Code Playgroud)

这是我的 FileZilla 日志:

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

请问有人有什么想法吗?

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

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()
Run Code Online (Sandbox Code Playgroud)

---> TLS 似乎没有启用,但为什么呢?

非常感谢 :)

Mar*_*ryl 5

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

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

请参阅https://docs.microsoft.com/en-us/dotnet/api/system.security.authentication.sslprotocols