Thank you @Martin for your answers.
- kuku17
Before posting, please read how to report bug or request support effectively.
Bug reports without an attached log file are usually useless.
The new key pair our IT guy has generated on our local machine is the User key pair, yes?
The remote server needs to also generate a Host key pair, keep its private key on its local machine and send us its public key, right? We then need to connect to the remote server using the username (without a password) , our User private key path and the fingerprint of the Host public key.
Also, are generated key pairs machine-bound? For example, if our IT guy has generated a key pair on one local server , is it safe to move them to another local server?
try {
string sFile = ConfigurationManager.AppSettings["sFile"];
string sFolder = ConfigurationManager.AppSettings["sFolder"];
string sftpHostName = ConfigurationManager.AppSettings["sftpHostName"];
string sPort = ConfigurationManager.AppSettings["sPort"];
string sUserName = ConfigurationManager.AppSettings["sUserName"];
string sPassword = ConfigurationManager.AppSettings["sPassword"];
string HostKeyFingerprint = ConfigurationManager.AppSettings["HostKeyFingerprint"];
string sessionLogPath = ConfigurationManager.AppSettings["sessionLogPath"];
string sshPrivateKeyPath = ConfigurationManager.AppSettings["sshPrivateKeyPath"];
SessionOptions sessionOptions = new SessionOptions();
sessionOptions.Protocol = Protocol.Sftp;
sessionOptions.FtpMode = FtpMode.Passive;
sessionOptions.HostName = sftpHostName;
sessionOptions.UserName = sUserName;
// sessionOptions.Password = sPassword;
sessionOptions.SshHostKeyFingerprint = HostKeyFingerprint;
sessionOptions.SshPrivateKeyPath = sshPrivateKeyPath;
sessionOptions.PortNumber = int.Parse(sPort);
using (Session session = new Session())
{
session.SessionLogPath = sessionLogPath + "\\log.txt";
Console.WriteLine("Starting Session...");
session.Open(sessionOptions);
Console.WriteLine("Session Opened");
// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
Console.WriteLine("Putting Files in remote folder started");
TransferOperationResult transferResult = session.PutFiles(sFile, sFolder, false, transferOptions);
transferResult.Check();
Console.WriteLine("Putting Files in remote folder ended");
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine(string.Format("Upload of {0} in secure ftp to {1} folder:{2} succeeded", transfer.FileName, sftpHostName, sFolder));
}
Console.ReadKey();
}
}
catch (Exception e)
{
Console.WriteLine("Exception occurred: " + e.Message);
Console.WriteLine("Inner Exception: " + e.InnerException?.Message);
Console.ReadKey();
}