2017-04-03 409 views
0

我正在創建一個用於從sftp服務器下載文件的窗​​口服務。爲此我使用Renci.SshNet,Renci.SshNet.CommonRenci.SshNet.Sftp使用PPK密鑰和密碼進行多因素身份驗證(非密碼)

我有這樣的代碼:

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(); 
    // ... 
} 

異常就說明是。

找不到合適的認證方法來完成認證。

據我所知這個錯誤是說我的憑證是錯誤的連接,但它不是這樣的。

我嘗試了以下解決方案,但都沒有工作。

我無法弄清楚什麼我缺少在我的代碼。

下面是我在使用FileZilla從客戶端計算機登錄到服務器時獲得的日誌文件。

2017-04-03 16:25:19 8120 3 Status: Connecting to abc.domainname.com... 
2017-04-03 16:25:19 8120 3 Trace: Going to execute "C:\Program Files\FileZilla FTP Client\fzsftp.exe" 
2017-04-03 16:25:19 8120 3 Response: fzSftp started 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::ConnectParseResponse(fzSftp started) 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::SendNextCommand() 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::ConnectSend() 
2017-04-03 16:25:19 8120 3 Command: keyfile "Z:\SFTP SETUP\CJ21_PVT.ppk" 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::ConnectParseResponse() 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::SendNextCommand() 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::ConnectSend() 
2017-04-03 16:25:19 8120 3 Command: keyfile "Z:\SFTP SETUP\CJ22_PVT.ppk" 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::ConnectParseResponse() 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::SendNextCommand() 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::ConnectSend() 
2017-04-03 16:25:19 8120 3 Command: keyfile "Z:\SFTP SETUP\CJ24_PVT.ppk" 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::ConnectParseResponse() 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::SendNextCommand() 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::ConnectSend() 
2017-04-03 16:25:19 8120 3 Command: keyfile "Z:\SFTP SETUP\CJ90_PVT.ppk" 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::ConnectParseResponse() 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::SendNextCommand() 
2017-04-03 16:25:19 8120 3 Trace: CSftpControlSocket::ConnectSend() 
2017-04-03 16:25:19 8120 3 Command: open "[email protected]" 22 
2017-04-03 16:25:19 8120 3 Trace: Looking up host "abc.domainname.com" 
2017-04-03 16:25:19 8120 3 Trace: Connecting to xxx.xxx.163.74 port 22 
2017-04-03 16:25:23 8120 3 Trace: Server version: SSH-2.0-1.82_sshlib Globalscape 
2017-04-03 16:25:23 8120 3 Trace: Using SSH protocol version 2 
2017-04-03 16:25:23 8120 3 Trace: We claim version: SSH-2.0-PuTTY_Local:_Mar_28_2014_10:34:48 
2017-04-03 16:25:24 8120 3 Trace: Doing Diffie-Hellman group exchange 
2017-04-03 16:25:24 8120 3 Trace: Doing Diffie-Hellman key exchange with hash SHA-1 
2017-04-03 16:25:25 8120 3 Trace: Host key fingerprint is: 
2017-04-03 16:25:25 8120 3 Trace: ssh-rsa 2048 6b:80:2c:5e:af:3f:2c:c7:f7:ef:4b:dd:85:55:32:fe 
2017-04-03 16:25:25 8120 3 Trace: Initialised AES-256 SDCTR client->server encryption 
2017-04-03 16:25:25 8120 3 Trace: Initialised HMAC-SHA1 client->server MAC algorithm 
2017-04-03 16:25:25 8120 3 Trace: Initialised AES-256 SDCTR server->client encryption 
2017-04-03 16:25:25 8120 3 Trace: Initialised HMAC-SHA1 server->client MAC algorithm 
2017-04-03 16:25:25 8120 3 Trace: Successfully loaded 4 key pairs from file 
2017-04-03 16:25:26 8120 3 Trace: Offered public key from "Z:\SFTP SETUP\CJ21_PVT.ppk" 
2017-04-03 16:25:26 8120 3 Trace: Server refused public key 
2017-04-03 16:25:26 8120 3 Trace: Offered public key from "Z:\SFTP SETUP\CJ22_PVT.ppk" 
2017-04-03 16:25:26 8120 3 Trace: Offer of public key accepted, trying to authenticate using it. 
2017-04-03 16:25:29 8120 3 Trace: Further authentication required 
2017-04-03 16:25:30 8120 3 Trace: Using keyboard-interactive authentication. 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: Access granted 
2017-04-03 16:25:30 8120 3 Trace: Opened channel for session 
2017-04-03 16:25:31 8120 3 Trace: Started a shell/command 
2017-04-03 16:25:31 8120 3 Status: Connected to abc.domainname.com 
2017-04-03 16:25:33 8120 3 Trace: CSftpControlSocket::ConnectParseResponse() 
2017-04-03 16:25:33 8120 3 Trace: CSftpControlSocket::ResetOperation(0) 
2017-04-03 16:25:33 8120 3 Trace: CControlSocket::ResetOperation(0) 
2017-04-03 16:25:33 8120 3 Trace: CFileZillaEnginePrivate::ResetOperation(0) 
2017-04-03 16:25:33 8120 3 Status: Retrieving directory listing... 
2017-04-03 16:25:33 8120 3 Trace: CSftpControlSocket::SendNextCommand() 
2017-04-03 16:25:33 8120 3 Trace: CSftpControlSocket::ChangeDirSend() 
2017-04-03 16:25:33 8120 3 Command: cd "/" 
2017-04-03 16:25:34 8120 3 Response: New directory is: "/" 
2017-04-03 16:25:34 8120 3 Trace: CSftpControlSocket::ResetOperation(0) 
2017-04-03 16:25:34 8120 3 Trace: CControlSocket::ResetOperation(0) 
2017-04-03 16:25:34 8120 3 Trace: CSftpControlSocket::ParseSubcommandResult(0) 
2017-04-03 16:25:34 8120 3 Trace: CSftpControlSocket::ListSubcommandResult() 
2017-04-03 16:25:34 8120 3 Trace: state = 1 
2017-04-03 16:25:34 8120 3 Trace: CSftpControlSocket::SendNextCommand() 
2017-04-03 16:25:34 8120 3 Trace: CSftpControlSocket::ListSend() 
2017-04-03 16:25:34 8120 3 Trace: state = 2 
2017-04-03 16:25:34 8120 3 Command: ls 
2017-04-03 16:25:34 8120 3 Status: Listing directory/
2017-04-03 16:25:36 8120 3 Listing: drwxrw-rw- 1 user group 0 Mar 24 2015 JKOB1 
2017-04-03 16:25:36 8120 3 Listing: drwxrw-rw- 1 user group 0 Apr 3 10:40 JKOB 
2017-04-03 16:25:36 8120 3 Trace: CSftpControlSocket::ListParseResponse() 
2017-04-03 16:25:36 8120 3 Trace: CSftpControlSocket::SendNextCommand() 
2017-04-03 16:25:36 8120 3 Trace: CSftpControlSocket::ListSend() 
2017-04-03 16:25:36 8120 3 Trace: state = 3 
2017-04-03 16:25:36 8120 3 Status: Calculating timezone offset of server... 
2017-04-03 16:25:36 8120 3 Command: mtime "JKOB" 
2017-04-03 16:25:37 8120 3 Response: 1491216040 
2017-04-03 16:25:37 8120 3 Trace: CSftpControlSocket::ListParseResponse(1491216040) 
2017-04-03 16:25:37 8120 3 Status: Timezone offsets: Server: 0 seconds. Local: 19800 seconds. Difference: 19800 seconds. 
2017-04-03 16:25:37 8120 3 Trace: CSftpControlSocket::ResetOperation(0) 
2017-04-03 16:25:37 8120 3 Trace: CControlSocket::ResetOperation(0) 
2017-04-03 16:25:37 8120 3 Status: Directory listing successful 
2017-04-03 16:25:37 8120 3 Trace: CFileZillaEnginePrivate::ResetOperation(0) 
2017-04-03 16:27:24 8120 3 Status: Disconnected from server 
2017-04-03 16:27:24 8120 3 Trace: CControlSocket::DoClose(64) 
2017-04-03 16:27:24 8120 3 Trace: CSftpControlSocket::ResetOperation(66) 
2017-04-03 16:27:24 8120 3 Trace: CControlSocket::ResetOperation(66) 
2017-04-03 16:27:24 8120 3 Trace: CFileZillaEnginePrivate::ResetOperation(66) 
2017-04-03 16:27:24 8120 3 Trace: CControlSocket::DoClose(64) 
2017-04-03 16:27:24 8120 3 Trace: CControlSocket::DoClose(64) 
2017-04-03 16:27:24 8120 3 Trace: CFileZillaEnginePrivate::ResetOperation(0) 

我試着從這裏和其他來源多個解決方案一個接一個,但都沒有工作。如果您有任何建議,那麼這是非常值得歡迎的。

+0

向我們展示一個日誌文件:https://github.com/sshnet/SSH.NET/wiki/Troubleshooting-SSH.NET#net-tracesource-and-tracelisteners –

+0

@MartinPrikryl我從哪裏可以得到日誌文件。? – Chirag

+0

您是否嘗試刪除它? –

回答

1

SSH.NET不支持.ppk密鑰文件。您必須使用PuTTYgen將.ppk鍵轉換爲OpenSSH格式。

雖然你應該得到「無效的私鑰文件。」例外,如果您嘗試使用.ppk文件。


原來的答覆,眼前的問題是編輯:

您正在使用FileZilla中多因素的私鑰和鍵盤交互認證:

2017年4月3日16:25: 26 8120 3跟蹤:從「Z:\ SFTP SETUP \ CJ22_PVT.ppk」提供公鑰「
2017-04-03 16:25:26 8120 3跟蹤:接受公鑰的提供,嘗試使用它進行身份驗證。
2017-04-03 16:25:29 8120 3跟蹤:需要進一步驗證
2017-04-03 16:25:30 8120 3跟蹤:使用鍵盤交互式驗證。inst_len:0,num_prompts:1
2017-04-03 16:25:30 8120 3命令:密碼:*********
2017-04-03 16:25:30 8120 3跟蹤:訪問授予

雖然,你正在使用簡單的密碼驗證在你的代碼:

using (var sftp = new SftpClient(Host, Port, Username, Password)) 

你怎麼能指望即使這個工作?


要實施multicator身份驗證,您必須使用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(); 

    // ... 
} 
+0

我也嘗試過這種方式......正如我在我的問題中所述。這是我嘗試過的解決方案之一,但它不工作.....它給出了同樣的錯誤。所以我在這裏提出問題的同時來到我的主要版本。 – Chirag

+0

你的問題包含完全無意義的代碼(關於你需要做什麼)。因此,向我們展示一些你已經嘗試過的明智的代碼。 –

+1

如何加載'.ppk'文件? SSH.NET不支持這種格式。 –

相關問題