KeyboardInteractiveAuthenticationMethod & SSH Private Key without Password

Jun 28, 2013 at 4:20 PM
Hello all,

I am trying to connect using an SSH Private Key File WITHOUT requiring a password.

Here is a code snippet:
                Hostname = HostnameText.Text;
                Username = UsernameText.Text;
                Password = PasswordText.Password;
                FileStream fs = File.Open(SSHKeyPath.Text, FileMode.Open);
                PrivateKeyFile SSHKey = new PrivateKeyFile(fs);
                Auth2 = new KeyboardInteractiveAuthenticationMethod(Username);
                Auth3 = new PrivateKeyAuthenticationMethod(Username);
                ServerInfo = new ConnectionInfo(Hostname, 22, Username, Auth2, Auth3);

                Ssh = new SshClient(ServerInfo);

                Auth2.AuthenticationPrompt += delegate(object s, AuthenticationPromptEventArgs ev)
                {
                    foreach (var prompt in ev.Prompts)
                    {
                        if (prompt.Request.Equals("Password: ", StringComparison.InvariantCultureIgnoreCase))
                        {
                            prompt.Response = Password; // "Password" acquired from resource
                        }
                    }
                };

               Ssh.Connect();
This works for me, but only if I provide a password. How can I do SSH Key Authentication without a password?
Jun 28, 2013 at 4:29 PM
Edited Jun 28, 2013 at 4:37 PM
maybe cause you don't use your SSHKey variable anywhere.

I think:
Auth3 = new PrivateKeyAuthenticationMethod(Username);
should be:
Auth3 = new PrivateKeyAuthenticationMethod(Username, SSHKey);
And btw. you don't have to do:
                FileStream fs = File.Open(SSHKeyPath.Text, FileMode.Open);
                PrivateKeyFile SSHKey = new PrivateKeyFile(fs);
this should be enough:
                PrivateKeyFile SSHKey = new PrivateKeyFile(SSHKeyPath.Text);
Also I'm not sure about the order of your auths. You want to prefer privatekey over password, so I think auth2 should be
PrivateKeyAuthenticationMethod and auth3 should be KeyboardInteractiveAuthenticationMethod.
Jun 28, 2013 at 6:45 PM
Thanks for the advice. As for the
 Auth3 = new PrivateKeyAuthenticationMethod(Username, SSHKey);
part, you are correct, I had that before but somehow left it out.

I agree with the
PrivateKeyFile SSHKey = new PrivateKeyFile(SSHKeyPath.Text);
suggestion as well.

I also switched the Auth order so that PrivateKeyAuthentication is first.

Here is my updated code snippet:
                Hostname = HostnameText.Text;
                Username = UsernameText.Text;
                Password = PasswordText.Password;
                PrivateKeyFile SSHKey = new PrivateKeyFile(SSHKeyPath.Text);
                Auth = new PasswordAuthenticationMethod(Username, Password);
                Auth2 = new PrivateKeyAuthenticationMethod(Username, SSHKey);
                Auth3 = new KeyboardInteractiveAuthenticationMethod(Username);
                
                ServerInfo = new ConnectionInfo(Hostname, 22, Username, Auth2, Auth3);

                Ssh = new SshClient(ServerInfo);

                Auth3.AuthenticationPrompt += delegate(object s, AuthenticationPromptEventArgs ev)
                {
                    foreach (var prompt in ev.Prompts)
                    {
                        if (prompt.Request.Equals("Password: ", StringComparison.InvariantCultureIgnoreCase))
                        {
                            prompt.Response = Password;
                        }
                    }
                };

                Ssh.Connect();
If I remove the KeyboardInteractiveAuthenticationMethod from the Connection Info, I get "No suitable authentication method found to complete authentication."

If I remove the AuthenticationPrompt Handler, I get "System.ArgumentNullException: Value cannot be null. Parameter name: data."

The issue still remains: How do I get establish the SSH connection WITHOUT needing a password?
Jun 28, 2013 at 6:54 PM
Edited Jun 28, 2013 at 7:03 PM
Are you sure you added your pubkey of the privatekey to $HOME/.ssh/authorized_keys of your target machine?

All I have to establish a connection with a private key is:
PrivateKeyFile privatekey = new PrivateKeyFile(path);
SshClient = new SshClient(Hostname, Port, SshUsername, privatekey );
SshClient.Connect();
The message "No suitable authentication method found to complete authentication." when you remove KeyboardInteractiveAuthenticationMethod means a auth with privatekey was not possible.

I bet it is an server issue. The code looks fine to me.

Edit:
And please check your sshd_config on the server if follwing is set:
RSAAuthentication yes
PubkeyAuthentication yes

Jun 28, 2013 at 9:18 PM
Are you sure you added your pubkey of the privatekey to $HOME/.ssh/authorized_keys of your target machine?
Thanks so much for the help! I had done that part of it incorrectly.
Jun 28, 2013 at 9:36 PM
No problem.
Glad to hear it is working now :)