问题描述
仅尝试使用当前SSH.NET库通过用户名和私钥进行身份验证。我无法从用户那里获得密码,所以这是不可能的。
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();
}
现在,如果我删除了 PasswordAuthenticationMethod $
AuthenticationMethod []
数组中的c $ c>我得到了一个异常,因为找不到合适的身份验证方法。如果我尝试这样传递(主机名,端口,用户名,密钥文件2)
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};
找不到合适的方法。
似乎如上所述,必须使用 ConnectionInfo
对象,但似乎它会评估 PasswordAuthenticationMethod
并无法登录(因为我没有提供密码)并且从不评估 PrivateKeyAuthMethod
...是这种情况吗?
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?
推荐答案
您的问题在这里吗?就是您仍在使用密码,即使密码为空。删除以下行:
Your problem here is that you're still using a password, even though it is blank. Remove this line:
new PasswordAuthenticationMethod(username, ""),
这对我来说很完美:
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());
这篇关于SSH.NET仅通过私钥进行身份验证(公钥身份验证)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!