Reputation: 299
I need to download a file from FTP thru WinSCP .NET assembly. I have this code currently but the error says Authentication failed.
try
{
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "172.xx.xxx.xx",
UserName = "usersample",
Password = "P@ssw0rd",
PortNumber = 21
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Download files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult =
session.GetFiles(
"/HST/sample.txt", "C:\\Users\\john\\Documents\\SampleFolder\\",
false, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Download of {0} succeeded", transfer.FileName);
}
Console.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
Console.ReadLine();
}
I got the code from this reference: https://winscp.net/eng/docs/library_session_getfiles
Originally, it has the following:
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
But I removed it since I don't have it and I think it's for SFTP (not sure) I've tried the credentials manually and they work. I am able to login. Please help. Thank you.
Upvotes: 5
Views: 12153
Reputation: 202642
There's WinSCP FAQ for your kind of situation:
Why I cannot connect/transfer using script, when I can using GUI (or vice versa)?
The easiest solution, to start with, is to use Generate transfer code function from your GUI session, to get a working code template.
Generate transfer code dialog opens. Select the .NET assembly tab and make sure C# language is selected.
Upvotes: 7
Reputation: 2182
You need SSH host key fingerprint in addition to your credentials for SSH connection.
You should get an SSH host key fingerprint along with your credentials from a server administrator. Knowing the host key fingerprint and thus being able to verify it is an integral part of securing an SSH connection. It prevents man-in-the-middle attacks.
This may help you to get your Host-Key:
https://winscp.net/eng/docs/faq_hostkey
https://winscp.net/eng/docs/ui_fsinfo
Upvotes: 0