0 votes
by (8.6k points)
edited

I'd like to authenticate to an SFTP server using my private key. The private key is not stored in the filesystem.

1 Answer

0 votes
by (8.6k points)
edited
 
Best answer

Following code shows how:

// create client and connect 
Sftp client = new Sftp();
client.Connect(hostname);

// instead of loading it from a disk file, load the private key from a byte array
//SshPrivateKey privateKey = new SshPrivateKey("key_rsa.pem", "password");

byte[] privateKeyData = YourMethodForObtainingPrivateKey();    
SshPrivateKey privateKey = new SshPrivateKey(privateKeyData, "password");

// authenticate 
client.Login(username, privateKey);

// ... 
...