首页 > 解决方案 > 尝试以编程方式连接时如何在 SFTP 服务器上接受警告消息

问题描述

我正在尝试连接到 FTP 服务器并收到以下错误消息:

找不到合适的身份验证方法来完成身份验证(公钥、键盘交互)。

我相信这是因为当我使用 SSH 安全 shell 登录服务器时,我从服务器收到一条警告消息。一旦我“确定”了这条消息,我就可以使用键盘交互式身份验证方法登录。如何以编程方式“确定”此消息?

这是我正在使用的代码。异常从sftp.Connect().

KeyboardInteractiveAuthenticationMethod keybAuth = new 
KeyboardInteractiveAuthenticationMethod(username);
keybAuth.AuthenticationPrompt += new 
    EventHandler<AuthenticationPromptEventArgs>(HandleKeyEvent);


ConnectionInfo coninfo = new ConnectionInfo(host, username, keybAuth);

using(var sftp = new SftpClient(host, username, password))
{
    sftp.Connect();

    var files = sftp.ListDirectory(pathRemotefile);

    for each (var file in files)
    {
        using (Stream filestream = File.OpenWrite(pathLocalFile))
        {
            sftp.DownloadFile(pathRemotefile, filestream);
        }
    }

    sftp.Disconnect();
}

PuTTY 事件日志

2019-08-07 10:40:42 Looking up host "****************" for SSH connection
2019-08-07 10:40:42 Connecting to ***.***.***.*** port 22
2019-08-07 10:40:42 We claim version: SSH-2.0-PuTTY_Release_0.72
2019-08-07 10:40:42 Remote version: SSH-2.0-OpenSSH_4.2
2019-08-07 10:40:42 We believe remote version has SSH-2 channel request bug
2019-08-07 10:40:42 Using SSH protocol version 2
2019-08-07 10:40:42 No GSSAPI security context available
2019-08-07 10:40:42 Doing Diffie-Hellman group exchange
2019-08-07 10:40:42 Doing Diffie-Hellman key exchange using 2048-bit modulus and hash SHA-1 (unaccelerated) with a server-supplied group
2019-08-07 10:40:42 Server also has ssh-dss host key, but we don't know it
2019-08-07 10:40:42 Host key fingerprint is:
2019-08-07 10:40:42 ssh-rsa 1024 d6:65:b9:53:c2:d4:54:91:28:5e:7b:b9:de:10:fe:12
2019-08-07 10:40:42 Initialised AES-256 SDCTR (AES-NI accelerated) outbound encryption
2019-08-07 10:40:42 Initialised HMAC-SHA-1 (unaccelerated) outbound MAC algorithm
2019-08-07 10:40:42 Initialised AES-256 SDCTR (AES-NI accelerated) inbound encryption
2019-08-07 10:40:42 Initialised HMAC-SHA-1 (unaccelerated) inbound MAC algorithm
2019-08-07 10:40:45 Attempting keyboard-interactive authentication
2019-08-07 10:41:09 Access granted
2019-08-07 10:41:09 Opening main session channel
2019-08-07 10:41:09 Opened main channel
2019-08-07 10:41:09 Allocated pty
2019-08-07 10:41:09 Started a shell/command
2019-08-07 10:41:09 Session sent command exit status 1
2019-08-07 10:41:09 Sent EOF message
2019-08-07 10:41:09 Main session channel closed
2019-08-07 10:41:09 All channels closed

腻子截图:

在此处输入图像描述

标签: c#.netsshsftp

解决方案


您在 SSH 身份验证横幅中描述的“警告”消息。

默认情况下,SSH.NET 会忽略横幅。除非你处理Session.UserAuthenticationBannerReceivedConnectionInfo.AuthenticationBanner. 即使您(错误地)处理它,您也永远不会得到“没有找到合适的身份验证方法来完成身份验证”


您的实际问题是,当您设置时KeyboardInteractiveAuthenticationMethod,您从不使用它,因为您coninfo的代码中从未使用过。

这应该有效:

var sftp = new SftpClient(coninfo)

推荐阅读