I have successfully used the open source SSH.NET library to work with SSH and SFTP.
This is the code to connect with keyfile + passphrase.
public void Connect(string host, int port, string user, string passPhrase, string privateKeyFilePath) { var keyFiles = new[] { new PrivateKeyFile(privateKeyFilePath, passPhrase) }; var methods = new List<AuthenticationMethod>(); methods.Add(new PasswordAuthenticationMethod(user, passPhrase)); methods.Add(new PrivateKeyAuthenticationMethod(user, keyFiles)); var con = new ConnectionInfo(host, port, user, methods.ToArray()); var client = new SshClient(con); client.Connect();
Private Key File Format
Please note that your private key file must be in OpenSSH format. If you open the key file in Notepad ++, it should have "BEGIN RSA PRIVATE KEY" on the first line.
If not, then convert your private key file to OpenSSH format using puttygen.
- Open private key in puttygen
- Go to the "Conversions" menu and select "Export OpenSSH Key."
- Save this new key in a file and use it.
source share