SSH.NET - No suitable authentication method found

前端 未结 2 983
无人及你
无人及你 2021-02-19 06:56

This is my code using SSH.NET

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

It works on

2条回答
  •  别跟我提以往
    2021-02-19 07:25

    I found the answer (at least for my problem, which seems to be the same as the op requested):

    I had to change the Authentication to KeyboardInteractiveAuthenticationMethod

    So this works now:

    KeyboardInteractiveAuthenticationMethod keybAuth = new KeyboardInteractiveAuthenticationMethod(SFTP_USR);
    keybAuth.AuthenticationPrompt += new EventHandler(HandleKeyEvent);
    
    ConnectionInfo conInfo = new ConnectionInfo(SFTP_HST, SFTP_PRT, SFTP_USR, keybAuth);
    
    using (SftpClient sftp = new SftpClient(conInfo))
    {
        sftp.Connect();
    
        // Do SFTP Stuff, Upload, Download,...
    
        sftp.Disconnect();
    }
    

    HandleKeyEvent then passes the Password:

    private void HandleKeyEvent(object sender, AuthenticationPromptEventArgs e)
    { 
        foreach (AuthenticationPrompt prompt in e.Prompts)
        {
            if (prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1)
            {
                prompt.Response = SFTP_PWD;
            }
        }
    }
    

提交回复
热议问题