Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Attempting to authenticate via username and privatekey only using the current SSH.NET library. I cannot get the password from the user so this is out of the question.

here is what i am doing now.

Renci.SshNet.ConnectionInfo conn = 
    new ConnectionInfo(hostName, port, username, new AuthenticationMethod[]
        {
            new PasswordAuthenticationMethod(username, ""), 
            new PrivateKeyAuthenticationMethod(username, new PrivateKeyFile[] 
                   { new PrivateKeyFile(privateKeyLocation, "") }),
        });

using (var sshClient = new SshClient(conn))
{
    sshClient.Connect();
} 

Now, if I remove the PasswordAuthenticationMethod from the AuthenticationMethod[] array I get an exception for for no suitable authentication method found. If i attempt to pass in the (hostname, port, username, keyfile2) as such

var keyFile = new PrivateKeyFile(privateKeyLocation);
var keyFile2 = new[] {keyFile};

again, no suitable method found.

It seems I have to use the ConnectionInfo object as I outlined above, but it seems that it evaluates the PasswordAuthenticationMethod and cant log in (since i don't provide a password) and never evaluates the PrivateKeyAuthMethod... is this the case? Is there some other way to authenticate with only a username or hostname and private key using SSH.NET lib?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
810 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your problem here is that you're still using a password, even though it is blank. Remove this line:

new PasswordAuthenticationMethod(username, ""), 

This works perfectly for me:

var pk = new PrivateKeyFile(yourkey);
var keyFiles = new[] { pk };

var methods = new List<AuthenticationMethod>();
methods.Add(new PrivateKeyAuthenticationMethod(UserName, keyFiles));

var con = new ConnectionInfo(HostName, Port, UserName, methods.ToArray());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...