提供文件内容为字符串而不是文件名

时间:2016-09-03 21:20:17

标签: c# ssh

我对C#很新,但不是编程。我尝试使用SSH.NET使用私钥建立SSH连接,使用的示例是here

public ConnectionInfo CreateConnectionInfo()
{
    const string privateKeyFilePath = @"C:\some\private\key.pem";    
    ConnectionInfo connectionInfo;
    using (var stream = new FileStream(privateKeyFilePath, FileMode.Open, FileAccess.Read))
    {
        var privateKeyFile = new PrivateKeyFile(stream);
        AuthenticationMethod authenticationMethod =
            new PrivateKeyAuthenticationMethod("ubuntu", privateKeyFile);

        connectionInfo = new ConnectionInfo(
            "my.server.com",
            "ubuntu",
            authenticationMethod);
    }

    return connectionInfo;
}

我没有把密钥存储在一个文件中,它来自另一个来源,我把它放在一个字符串中。我意识到我可以将它写入一个临时文件,然后传递该文件名,然后删除该文件,但我不想在可能的情况下将其写入磁盘。我没有在文档中看到任何允许我为此传递字符串的内容,只有文件名。我尝试过这样的方法将字符串转换为流:

byte[] private_key     = Encoding.UTF8.GetBytes(GetConfig("SSHPrivateKey"));
var private_key_stream = new PrivateKeyFile(new MemoryStream(private_key));

然后将private_key_stream作为PrivateKeyAuthenticationMethod的第二个参数传递。似乎没有来自编译器的抱怨,但该方法似乎并没有真正获得密钥(SSH没有进行身份验证,并且使用此密钥到服务器的PuTTY等外部尝试也能正常工作),所以看起来我错过了一些东西。

关于如何在不写出临时文件的情况下完成此任务的任何想法?

0 个答案:

没有答案
相关问题