Mar*_*ryl 13
可能每个 SFTP/SSH 库都支持公钥认证。
例如:
var privateKey = new PrivateKeyFile(@"C:\some\path\key.pem");
var client = new SftpClient("example.com", "username", new[] { privateKey });
client.Connect();
Run Code Online (Sandbox Code Playgroud)
如果私钥是加密的:
var privateKey = new PrivateKeyFile(@"C:\some\path\key.pem", "passphrase");
Run Code Online (Sandbox Code Playgroud)
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "example.com",
UserName = "username",
SshHostKeyFingerprint = "ssh-rsa 2048 ...=",
SshPrivateKeyPath = @"C:\some\path\key.ppk",
};
using (Session session = new Session())
{
session.Open(sessionOptions);
// Your code
}
Run Code Online (Sandbox Code Playgroud)
WinSCP 需要将密钥转换为 PPK 格式(您可以使用 WinSCP GUI 或 PuTTYgen)。另请注意,WinSCP 会验证 SSH 主机密钥 ( SshHostKeyFingerprint)。默认情况下 SSH.NET 无法做到这一点,这是一个安全漏洞。
如果私钥已加密,请添加PrivateKeyPassphrase或SecurePrivateKeyPassphrase。
WinSCP GUI 可以为您生成代码模板。
(我是图书馆的作者)
| 归档时间: |
|
| 查看次数: |
14364 次 |
| 最近记录: |