2015-06-05 86 views
1

試圖通過用戶名和私鑰僅使用當前的ssh.net庫進行身份驗證。我無法從用戶那裏獲得密碼,所以這是不可能的。ssh.net僅通過私鑰進行身份驗證

這是我現在正在做的事情。

Renci.SshNet.ConnectionInfo conn = new ConnectionInfo(hostName, port, username, new AuthenticationMethod[] 
     { 
      new PasswordAuthenticationMethod(username, ""), 
      new PrivateKeyAuthenticationMethod(username, new PrivateKeyFile[] { new PrivateKeyFile(privateKeyLocation, "") }), 
     }); 

     using (var sshClient = new SshClient(conn)) 
     { 
      sshClient.Connect(); 

}

現在,如果i。從AuthenticationMethod[]陣列I得到一個異常用於發現合適的認證方法除去PasswordAuthenticationMethod。如果我試圖通過(主機名,端口,用戶名,密鑰文件2)這樣的

var keyFile = new PrivateKeyFile(privateKeyLocation); 
var keyFile2 = new[] {keyFile}; 

再次,找不到合適的方法。

看來我必須使用ConnectionInfo對象,因爲我上面列出,但似乎它評估PasswordAuthenticationMethod和無法登陸(因爲我不提供密碼),從來沒有評估PrivateKeyAuthMethod ...這是案子?有沒有其他的方法來驗證只有用戶名或主機名和私鑰使用ssh.net lib?

回答

0

這裏你的問題是你仍然在使用密碼,即使它是空白的。刪除這一行:

new PasswordAuthenticationMethod(username, ""), 

這完美的作品對我來說:

  var pk = new PrivateKeyFile(yourkey); 
      var keyFiles = new[] { pk }; 

      var methods = new List<AuthenticationMethod>(); 
      methods.Add(new PrivateKeyAuthenticationMethod(UserName, keyFiles)); 

      var con = new ConnectionInfo(HostName, Port, UserName, methods.ToArray()); 
+0

啊,是的,這對我有效,謝謝! –

0

要執行私鑰驗證,您還需要密碼,它與私鑰一起允許驗證。
真正需要的是RenciSSH與時俱進並寫出一種公認的認證方法。

0

你需要一些這樣的事,對我來說工作正常。 創建新的ConnectionInfo對象時請注意我只傳遞主機,端口,用戶和認證方法(不需要密碼); 第二個區別是我傳遞了單個PrivateKeyFile,沒有用於'Phrase'參數的空引號;

public ConnectionInfo GetCertificateBasedConnection() 
    { 
     ConnectionInfo connection; 
     Debug.WriteLine("Trying to create certification based connection..."); 
     using (var stream = new FileStream(ConfigurationHelper.PrivateKeyFilePath, FileMode.Open, FileAccess.Read)) 
     { 
      var file = new PrivateKeyFile(stream); 
      var authMethod = new PrivateKeyAuthenticationMethod(ConfigurationHelper.User, file); 

      connection = new ConnectionInfo(ConfigurationHelper.HostName, ConfigurationHelper.Port, ConfigurationHelper.User, authMethod); 
     } 
     Debug.WriteLine("Certification based connection created successfully"); 
     return connection; 
    } 
相關問題