尝试以编程方式连接时如何接受 SFTP 服务器上的警告消息

How do I accept warning message on SFTP server when trying to connect programmatically

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

No suitable authentication method found to complete authentication (public key,keyboard-interactive).

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

这是我正在使用的代码。 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

PuTTY 屏幕截图:

您在 SSH 身份验证横幅中描述的 "warning" 消息。

横幅默认被 SSH.NET 忽略。除非你处理 Session.UserAuthenticationBannerReceivedConnectionInfo.AuthenticationBanner。即使你处理它(不正确),你也永远不会得到 "No suitable authentication method found to complete authentication".


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

这应该有效:

var sftp = new SftpClient(coninfo)