我正在创建一个窗口服务,用于从 SFTP 服务器下载文件。为此,我正在使用Renci.SshNet,Renci.SshNet.Common和Renci.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)
我得到例外。
私钥文件无效。
我无法弄清楚我的代码中缺少什么。
下面是我使用 …
我对 SFTP 或 OpenSSH 没有经验。我正在尝试连接到客户端的 SFTP 以上传文件。
我正在使用 SSH.NET 库 – https://github.com/sshnet/SSH.NET
在我的 C# 代码中,我有一个字符串变量中的私钥:
var key = @"-----BEGIN OPENSSH PRIVATE KEY-----
// snipped
-----END OPENSSH PRIVATE KEY-----
";
MemoryStream keyStream = new MemoryStream(Encoding.UTF32.GetBytes(key));
Renci.SshNet.ConnectionInfo conn = new Renci.SshNet.ConnectionInfo(
host,
port,
username,
new AuthenticationMethod[]
{
new PrivateKeyAuthenticationMethod(username, new PrivateKeyFile[]
{
new PrivateKeyFile(keyStream, ""),
}),
});
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
无效的私钥文件。
我的问题是,我已经看到其他键开始BEGIN RSA但我的键开始BEGIN OPENSSH- 这是问题吗?