在 SSH.NET 中使用 PPK 密钥进行身份验证

Chi*_*rag 9 .net c# ssh sftp ssh.net

我正在创建一个窗口服务,用于从 SFTP 服务器下载文件。为此,我正在使用Renci.SshNet,Renci.SshNet.CommonRenci.SshNet.Sftp.

我有这个代码:

String Host = "HostName";
int Port = 22;
String RemoteFileDirectory =
    Convert.ToString(ConfigurationManager.AppSettings["SourcePath"]);
String Username = "UserName";
String Password = "*******";

var KeybasedMethod = new KeyboardInteractiveAuthenticationMethod(Username);
KeybasedMethod.AuthenticationPrompt +=
    (sender, e) => { e.Prompts.First().Response = password; };

AuthenticationMethod[] methods = new AuthenticationMethod[] 
{
    new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(@"Z:\SFTP SETUP\CJ22")),
    KeybasedMethod
};
ConnectionInfo connectionInfo = new ConnectionInfo(hostname, username, methods);

using (var sftp = new SftpClient(connectionInfo))
{
    sftp.Connect();
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我得到例外。

私钥文件无效。

我无法弄清楚我的代码中缺少什么。

下面是我使用 FileZilla 从客户端计算机登录服务器时收到的日志文件。

String Host = "HostName";
int Port = 22;
String RemoteFileDirectory =
    Convert.ToString(ConfigurationManager.AppSettings["SourcePath"]);
String Username = "UserName";
String Password = "*******";

var KeybasedMethod = new KeyboardInteractiveAuthenticationMethod(Username);
KeybasedMethod.AuthenticationPrompt +=
    (sender, e) => { e.Prompts.First().Response = password; };

AuthenticationMethod[] methods = new AuthenticationMethod[] 
{
    new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(@"Z:\SFTP SETUP\CJ22")),
    KeybasedMethod
};
ConnectionInfo connectionInfo = new ConnectionInfo(hostname, username, methods);

using (var sftp = new SftpClient(connectionInfo))
{
    sftp.Connect();
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我已经从这里和其他来源尝试了多种解决方案,但没有一个有效。如果您有任何建议,我们非常欢迎。

Mar*_*ryl 9

SSH.NET 不支持 .ppk 密钥文件。您必须使用 PuTTYgen 将 .ppk 密钥转换为 OpenSSH 格式。

请参阅如何将使用 PuTTYgen (Windows) 生成的 SSH 密钥对转换为 ssh-agent 和 Keychain (Linux) 使用的密钥对


编辑问题之前的原始答案:

您正在 FileZilla 中使用多因素私钥和键盘交互式身份验证:

2017-04-03 16:25:26 8120 3 Trace:从“Z:\SFTP SETUP\CJ22_PVT.ppk”提供公钥
2017-04-03 16:25:26 8120 3 Trace:接受公钥提供,正在尝试使用它进行身份验证。
2017-04-03 16:25:29 8120 3 Trace:需要进一步身份验证
2017-04-03 16:25:30 8120 3 Trace:使用键盘交互式身份验证。inst_len: 0, num_prompts: 1
2017-04-03 16:25:30 8120 3 Command: Pass: *********
2017-04-03 16:25:30 8120 3 Trace: 授予访问权限

同时,您在代码中使用简单的密码身份验证:

using (var sftp = new SftpClient(Host, Port, Username, Password))
Run Code Online (Sandbox Code Playgroud)

你怎么能指望这能起作用呢?


要实现多重身份验证,您必须使用ConnectionInfo.

var keybInterMethod = new KeyboardInteractiveAuthenticationMethod(username);
keybInterMethod.AuthenticationPrompt +=
    (sender, e) => { e.Prompts.First().Response = password; };

AuthenticationMethod[] methods = new AuthenticationMethod[] {
    new PrivateKeyAuthenticationMethod(username, new PrivateKeyFile(privateKey)),
    keybInterMethod
};
ConnectionInfo connectionInfo = new ConnectionInfo(hostname, username, methods);

using (var sftp = new SftpClient(connectionInfo))
{
    sftp.Connect();

    // ...
}
Run Code Online (Sandbox Code Playgroud)