SSH.NET Authenticate via private key only (public key authentication)

前端 未结 3 1368
情话喂你
情话喂你 2021-01-05 08:41

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.

3条回答
  •  天命终不由人
    2021-01-05 09:25

    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();
    methods.Add(new PrivateKeyAuthenticationMethod(UserName, keyFiles));
    
    var con = new ConnectionInfo(HostName, Port, UserName, methods.ToArray());
    

提交回复
热议问题