Reputation: 22516
I have to communicate to an FTP server with SSL.
I've received an example on how to do that that uses, I believe, the WinScp command:
open ftpes://SomeUser:[email protected]/ -certificate="xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
The question is how to use the -certificate
part in an application? Do I have to save the string in a file or...?
For example if we use C#'s FtpWebRequest:
FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(downlaodLocation);
how can we use this fingerprint?
X509Certificate class has a constructor that accepts byte[].
byte[] toBytes = Encoding.ASCII.GetBytes("xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx");
ftp.ClientCertificates.Add(new X509Certificate(toBytes));
Is this the right way to use the certificate fingerprint?
Upvotes: 3
Views: 1630
Reputation: 22516
Eventually I took a different approach for uploading the file and used WinScp for c# lib for that.
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = ConfigurationManager.AppSettings["FTP.HOSTNAME"],
UserName = ConfigurationManager.AppSettings["FTP.USERNAME"],
Password = ConfigurationManager.AppSettings["FTP.PASSWORD"],
};
if (port.HasValue)
{
sessionOptions.PortNumber = port.Value;
}
sessionOptions.SshHostKeyFingerprint = ConfigurationManager.AppSettings["FTP.CERT.FINGERPRINT"].Trim();
using (Session session = new Session())
{
if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["LOG.PATH"]))
{
session.SessionLogPath = ConfigurationManager.AppSettings["LOG.PATH"];
}
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions
{
TransferMode = TransferMode.Binary
};
TransferOperationResult transferResult = session.PutFiles(ConfigurationManager.AppSettings["FILE.TO.UPLOAD"], ConfigurationManager.AppSettings["FILE.DESTINATION.NAME"], true, transferOptions);
transferResult.Check();
StringBuilder result = new StringBuilder();
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
result.Append(string.Format("Upload of {0} : {1}", transfer.FileName, transfer.Error));
}
Console.WriteLine(result.ToString());
}
Upvotes: 1