Anyone with experience in connecting to SFTPs with C#, with RSA keys then I'd much appreciate their expertees.
I already have some C# programs that connect to an SFTP using RSA keys, and have successfully connected with FileZilla in this instance, so I'm not sure exactly what is not working properly.
I have 'PublicKey.key' and 'PrivateKey.ppk', and when I connect by using the private key on Filezilla, it works/opens fine.
The following code is taken from another connection (different key set, but works fine):
<add key="keyDIR" value="C:\Users\Administrator\Dropbox\NET\test2d.ppk" />
using Renci.SshNet;
SftpClient xclient = null;
string sPrivateKeyPath = ConfigurationManager.AppSettings["keyDIR"];
PrivateKeyFile ObjPrivateKey = null;
PrivateKeyAuthenticationMethod ObjPrivateKeyAutentication = null;
using (Stream stream = File.OpenRead(sPrivateKeyPath))
{
if (ConfigurationManager.AppSettings["PassPhraseCode"] != null)
{
string sPassPhrase = ConfigurationManager.AppSettings["PassPhraseCode"];
ObjPrivateKey = new PrivateKeyFile(stream, sPassPhrase);
ObjPrivateKeyAutentication = new PrivateKeyAuthenticationMethod(pub.JBuser, ObjPrivateKey);
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " '" + stream.ToString() + "'");
And it is the "new PrivateKeyFile" line in which the code breaks. The error I am receiving is "The Application is in Break Mode". Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code)."
I have used Console.WriteLine.. to check the directory / password, and am confident it is directing to the correct place.
Only thing I can think of is something to do with the key specifications perhaps? Unfortunately I cannot remember what specifications I selected when creating it (months ago now). The keys were created using PuTTy.
Solution: It was related to the fact that filetype of my key '.ppk' cannot work with SSH.NET. The following was used to convert it:
- Open PuttyGen
- Click Load
- Load your private key
- Go to Conversions->Export OpenSSH and export your private key