我无法在 .NET 应用程序中使用 Renci.SshNet 库远程连接到 Mac 系统 (OS X 10.10.5)。我使用的代码允许我连接到任何其他 linux 操作系统系统,如 RHL、Ubuntu 等。我可以通过腻子连接到这个 Mac 系统。但是当我尝试从我的 .NET 应用程序连接时,它给了我以下错误 -
找不到合适的身份验证方法来完成身份验证(公钥、键盘交互)。来源 - Renci.SshNet
这是我的代码。请帮忙
public SshClient sshClient;
sshClient = new SshClient(ipAddress, port, username, password);
sshClient.KeepAliveInterval = TimeSpan.FromSeconds(60);
sshClient.Connect();
Run Code Online (Sandbox Code Playgroud)
基于对 stackoverflow 的各种贡献,认为 mac 要求进行键盘身份验证,而我的方法只有基于密码的身份验证。所以这个更新的代码可以同时......感谢所有贡献者......
public SshClient sshClient;
KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(username);
PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(username, password);
kauth.AuthenticationPrompt += new EventHandler<Renci.SshNet.Common.AuthenticationPromptEventArgs>(HandleKeyEvent);
ConnectionInfo connectionInfo = new ConnectionInfo(ipAddress, port, username, pauth, kauth);
sshClient = new SshClient(connectionInfo);
sshClient.KeepAliveInterval = TimeSpan.FromSeconds(60);
sshClient.Connect();
Run Code Online (Sandbox Code Playgroud)
以及密码提示事件的此事件处理程序代码
void HandleKeyEvent(Object sender, Renci.SshNet.Common.AuthenticationPromptEventArgs e)
{
foreach (Renci.SshNet.Common.AuthenticationPrompt prompt in e.Prompts)
{
if (prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1)
{
prompt.Response = password;
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4965 次 |
最近记录: |