简体   繁体   English

无法使用WinSCP .NET程序集从FTP下载文件的代码无法连接,但是我可以在WinSCP GUI中进行连接

[英]Code to download file from FTP with WinSCP .NET assembly does not connect, but I can connect in WinSCP GUI

I need to download a file from FTP thru WinSCP .NET assembly. 我需要通过WinSCP .NET程序集从FTP下载文件。 I have this code currently but the error says Authentication failed. 我目前有此代码,但错误提示身份验证失败。

try
{
    SessionOptions sessionOptions = new SessionOptions
    {
        Protocol = Protocol.Ftp,
        HostName = "172.xx.xxx.xx",
        UserName = "usersample",
        Password = "P@ssw0rd",
        PortNumber = 21
    };

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

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

        TransferOperationResult transferResult;
        transferResult =
            session.GetFiles(
                "/HST/sample.txt", "C:\\Users\\john\\Documents\\SampleFolder\\",
                false, transferOptions);

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

        // Print results
        foreach (TransferEventArgs transfer in transferResult.Transfers)
        {
            Console.WriteLine("Download of {0} succeeded", transfer.FileName);
        }

        Console.ReadLine();
    }

}
catch (Exception e)
{
    Console.WriteLine("Error: {0}", e);
    Console.ReadLine();
}

I got the code from this reference: https://winscp.net/eng/docs/library_session_getfiles 我从此参考中获得了代码: https : //winscp.net/eng/docs/library_session_getfiles

Originally, it has the following: 最初,它具有以下功能:

SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"

But I removed it since I don't have it and I think it's for SFTP (not sure) I've tried the credentials manually and they work. 但是我删除了它,因为我没有它,而且我认为它是针对SFTP的(不确定),我已经手动尝试过凭据,它们可以工作。 I am able to login. 我可以登录。 Please help. 请帮忙。 Thank you. 谢谢。

There's WinSCP FAQ for your kind of situation: 针对您的情况有WinSCP常见问题解答:
Why I cannot connect/transfer using script, when I can using GUI (or vice versa)? 为什么可以使用GUI时无法使用脚本进行连接/传输(反之亦然)?


The easiest solution, to start with, is to use Generate transfer code function from your GUI session, to get a working code template. 首先,最简单的解决方案是在GUI会话中使用“ 生成传输代码”功能,以获得有效的代码模板。

  • Login in GUI; 登录GUI;
  • Select the file to download in remote panel and the target local directory in local panel; 在远程面板中选择要下载的文件,在本地面板中选择目标本地目录;
  • Use Download command; 使用下载命令;
  • On the Download transfer options dialog , use Transfer settings > Generate Code ; 下载传输选项对话框中 ,使用传输设置>生成代码 ;
  • Generate transfer code dialog opens. 生成传输代码对话框打开。 Select the .NET assembly tab and make sure C# language is selected. 选择.NET程序集选项卡 ,并确保选择了C#语言。

    在此处输入图片说明

You need SSH host key fingerprint in addition to your credentials for SSH connection. 除了用于SSH连接的凭据之外,您还需要SSH主机密钥指纹。

You should get an SSH host key fingerprint along with your credentials from a server administrator. 您应该从服务器管理员处获得SSH主机密钥指纹以及凭据。 Knowing the host key fingerprint and thus being able to verify it is an integral part of securing an SSH connection. 知道主机密钥指纹并因此能够对其进行验证是保护SSH连接不可或缺的一部分。 It prevents man-in-the-middle attacks. 它可以防止中间人攻击。

This may help you to get your Host-Key: 这可以帮助您获取主机密钥:

https://winscp.net/eng/docs/faq_hostkey https://winscp.net/eng/docs/faq_hostkey

https://winscp.net/eng/docs/ui_fsinfo https://winscp.net/eng/docs/ui_fsinfo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM