SSH.NET 仅通过私钥进行身份验证(公钥身份验证) [英] SSH.NET Authenticate via private key only (public key authentication)

查看:32
本文介绍了SSH.NET 仅通过私钥进行身份验证(公钥身份验证)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅使用当前的 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();
} 

现在,如果我从 AuthenticationMethod[] 数组中删除 PasswordAuthenticationMethod,我会收到一个异常,因为找不到合适的身份验证方法.如果我尝试传入 (hostname, port, username, keyfile2)

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.

似乎我必须使用上面概述的 ConnectionInfo 对象,但它似乎评估了 PasswordAuthenticationMethod 并且无法登录(因为我没有提供密码)并且从不评估 PrivateKeyAuthMethod... 是这种情况吗?是否有其他方法可以使用 SSH.NET lib 仅通过用户名或主机名和私钥进行身份验证?

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 仅通过私钥进行身份验证(公钥身份验证)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆