SSH.NET Public Key Authentication [duplicate]

随声附和 提交于 2019-12-23 05:32:20

问题


I know that private key authentication works, however I'm looking for public key authentication.

I'm trying to establish a connection using SSH.NET and a public key.

In the Git Bash terminal I can connect and run commands fine using my public key, but when I try connecting to the same host using SSH.NET I get an exception.

Here is my how I'm trying to connect:

using (var client = new SshClient("host.name.com", port, "username"))
{
    client.Connect(); // exception thrown here
}

I've also tried this route:

var authMethod = new PrivateKeyAuthenticationMethod("username");
var info = new ConnectionInfo("host.name.com", port, "username", authMethod);
using (var client = new SshClient(info))

Both give the same exception: Permission denied (publickey).

It seems as though I need to specify where my public key is, or I need to put my public key in a certain spot, but I can't find any documentation telling me where to put it.


回答1:


There's no "private key authentication". It's actually called "public key authentication".

Though you need both private and public key to authenticate using "public key authentication". It's called "public key authentication", because a client (SSH.NET in this case) sends only the public key to the server – So the server authenticates you using the public key only. The private key is used locally only.

Though file formats that are commonly referred to as a private key (like PEM or .ppk) – as opposite to public key (.pub) formats – actually contain whole key pair (both public and private key).

So the question that you link to – SSH.NET Authenticate via private key only (public key authentication) – actually does what you want. It shows how to authenticate using "public key authentication" – using private key file format (both public and private key really). I've edited the title of that question to make this more clear.


And to correct you, it's not true that "In the Git Bash terminal I can connect and run commands fine using my public key" – That's not possible. You must have a private key too.



来源:https://stackoverflow.com/questions/53344409/ssh-net-public-key-authentication

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!