.NET - 使用 .pem 文件连接到 ssh

问题描述 投票:0回答:0

我正在尝试使用 SSH.NET 连接到服务器。

我有- 主机、用户、.pem 文件。

用putty就可以了(我做了一个.ppk文件), 但是当我尝试使用 C# 连接到服务器时,生活变得一团糟。

这是我的代码——我不知道它不起作用。 最后我收到以下错误(尝试连接时)

Renci.SshNet.Common.SshAuthenticationException: 'Permission denied (publickey).'

这是我的代码.. 有什么建议吗? 10 倍很多!

`公共字符串 keyString = @" -----开始 RSA 私钥----- 等等等等等等 -----结束 RSA 私钥-----";

        PrivateKeyFile privateKey;

        using (var keystrm = new MemoryStream(Encoding.ASCII.GetBytes(keyString)))
        {
            privateKey = new PrivateKeyFile(keystrm);
        }

        var keyFiles = new[] { privateKey };

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

        var con = new ConnectionInfo(host, username, methods.ToArray());

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

我尝试使用文件流将 pem 读取到文件中,但都遇到了同样的错误。

.net ssh connection
© www.soinside.com 2019 - 2024. All rights reserved.