Upload file via FTP TLS

2

I'm trying to upload files via FTP (TLS), but I'm not successful because it always returns an error when my code executes "Connect ()". I have already tried using the FtpWebRequest, FtpClient, Tamir.SharpSsh, and SftpClient classes. Any of them during the "Connect ()" statement get a connection failure message, it follows detail:

a) Connect with classes to SFTP Example:

    private void UploadSFTPFile(string host, string username, string 
           password, string sourcefile, string destinationpath, string port)
    {
        int port2 = Convert.ToInt32(port);

        try
        {
            using (SftpClient client = new SftpClient(host, port2, username, password))
            {
                client.Connect();  //o erro ocorre aqui!!
                client.ChangeDirectory(destinationpath);

                using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
                {
                    try
                    {
                        client.BufferSize = 4 * 1024;
                        client.UploadFile(fs, Path.GetFileName(sourcefile));
                        Global.retorno = "Envio via SFTP " + host + " efetuado com sucesso.";
                    }

                    catch (Exception ex)
                    {
                        Global.retorno = "Erro no envio do arquivo via SFTP (1)" + host + ". Detalhe: " + ex;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Global.retorno = "Erro na conexão via SFTP (2)" + host + ". Detalhe: " + ex;
        }
    }

Error Message:  "A connection attempt failed because the connected component did not respond correctly after a period of time or the established connection failed because the connected host did not respond ..."

IMPORTANT: I also tried to use the class / library "WinSCP", in this case I had the following return message: "Connection failed. Authentication failed. Connection failed. TLS required".

The strange thing is that with FileZila I connect without problems informing the host, userName and pasaword. I do not need to enter a key or certificate and I connect normally. So I'm not clear about what's happening.

Should I use a key / certificate to connect? Even Filizila does not need this ...

Obs.Framework 4.5.2

Thank you in advance.

    
asked by anonymous 25.11.2018 / 01:43

2 answers

2

Yes, but this is not a simple FTP, it asks for a public key (TLS) which I was not aware of, I just found the solution. I basically used WinSCP ( link ). What I believe to be relevant for anyone who has to go through the same problem (because I have suffered to find out), is that within Filezila it is possible to discover the public key and use it in the code as below (tkey):

    private void UploadFTPTLSFile(string host, string username, string password, string sourcefile, string destinationpath, string port, string tkey)
    {
        //https://winscp.net/eng/docs/library

        int port2 = Convert.ToInt32(port);
        destinationpath = destinationpath + "\" + arquivoUpFtpChk;

        try
        {
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Ftp,
                HostName = host,
                UserName = username,
                Password = password,
                FtpSecure = FtpSecure.Implicit,
                TlsHostCertificateFingerprint = tkey //aqui vai a chave pública que pode ser descoberta a qual o Filezila utiliza...
            };

            using (WinSCP.Session session = new WinSCP.Session())
            {
                // Connect
                session.Open(sessionOptions);

                // Upload files
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;

                TransferOperationResult transferResult;
                transferResult =
                    session.PutFiles(sourcefile, destinationpath, false, transferOptions);

                // Throw on any error
                transferResult.Check();

                // Print results
                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    Global.retorno = ("Upload efetuado com sucesso:" + transfer.FileName);
                }
            }
        }

        catch (Exception ex)
        {
            Global.retorno = ("Erro: " + ex);
        }
    }
    
03.12.2018 / 18:10
1

You've already tried using FtpWebRequest this way?

FtpWebRequest requestFTP = (FtpWebRequest)WebRequest.Create(urlFTP);
requestFTP.Credentials = new NetworkCredential(usuario, senha);
requestFTP.EnableSsl = true;
    
27.11.2018 / 16:13