sFTP with privateKeyFile and password in ftp connection

May 6, 2014 at 9:36 AM
With SFtpClient. I need a connection mix PrivateConnectionInfo and password in sftpClient. I have a method with host, port, username and password and another method with host, port, username and PrivatekeyFile. But I haven´t a method with both password and PrivateKeyFile.
I have tried with PasswordConnectionInfo but I can´t attach to SftpClient when it attached the PrivateKeyFile.
How can I solve this inconvenient and attach both password FTP and PrivateKeyFile.
Coordinator
May 7, 2014 at 7:56 PM
Edited May 7, 2014 at 7:57 PM
You should use the SftpClient constructor that takes a ConnectionInfo:
var authenticationMethods =
    {
        new PasswordAuthenticationMethod("user", "pwd"),
        new PrivateKeyAuthenticationMethod("user", "key.pem")
    };

using (var client = new SftpClient(new ConnectionInfo("host", 22, "user", authenticationMethods)))
{
    ...
}
Cheers,
Gert
Marked as answer by drieseng on 5/7/2014 at 1:00 PM
May 7, 2014 at 9:28 PM
Thatks drieseng.
Jun 9, 2015 at 2:27 PM
What if you do not or cannot use a PasswordAuthenticationMethod? In my case i JUST want to use the private key. Is there a method or procedure for accomplishing this?
Jun 9, 2015 at 6:24 PM
Edited Jun 9, 2015 at 6:30 PM
simply like this
vb.net:
' your openssh private key
dim Ppk as string = "-----BEGIN RSA PRIVATE KEY-----" & vbcrlf _
                               &    ......................................................  & vbcrlf _
                               & "-----END RSA PRIVATE KEY-----"
dim UserCode as string

' --------- SSH -------------
dim SSH_Socket As Renci.SshNet.SshClient= New Renci.SshNet.SshClient(<Host>, UserCode, _
                                New Renci.SshNet.PrivateKeyFile(New MemoryStream(Encoding.Default.GetBytes(Ppk))))
SSH_Socket.Connect()
if SSH_Socket.isConnected then
..................
endif

' ---------- FTP -------------

dim SFTP_Socket as Renci.SshNet.SftpClient = New Renci.SshNet.SftpClient(<Host>, UserCode, _
                                 New Renci.SshNet.PrivateKeyFile(New MemoryStream(Encoding.Default.GetBytes(Ppk))))
SFTP_Socket .Connect()
if SFTP_Socket.isConnected then
..................
endif


Hopes this will help