I have been assigned to a project wherein I am required to do SFTP (or a secure file transfer) from a local computer to a remote computer. The application is a C# .NET Console app and some of you may be looking for the perfect way to assist your .NET development with SFTP.
So upon my research, I encountered the following links that could help you.
Development Library: SSH.NET
This is a third party library that you can use to deal with SSH and SFTP transactions. It is downloadable for free from SourceForge.net, and requires no installation. Just add the Renci.SshNet.dll as a reference to the project.
Sample code for SFTP upload:
Sample code for SFTP download:
So upon my research, I encountered the following links that could help you.
Development Library: SSH.NET
This is a third party library that you can use to deal with SSH and SFTP transactions. It is downloadable for free from SourceForge.net, and requires no installation. Just add the Renci.SshNet.dll as a reference to the project.
Sample code for SFTP upload:
var connectionInfo = new ConnectionInfo(remotesvr, port, usrnme, new PasswordAuthenticationMethod(usrnme, usrpwd), new PrivateKeyAuthenticationMethod(usrnme, privateKey));
using (var sftp = new SftpClient(connectionInfo)) { sftp.Connect(); using (var file = File.OpenRead(zipfilename)) { sftp.UploadFile(file, string.Concat("./", zipfilename)); } sftp.Disconnect(); }
Sample code for SFTP download:
string root = appSettings["DeliveryNotificationsFolder"].ToString(); using (var sftp = new SftpClient(connectionInfo)) { sftp.Connect(); foreach (SftpFile sftpFile in sftp.ListDirectory("./UploadFiles/")) //get files from remote folder { if (!sftpFile.Name.Contains(".zip")) continue;
using (var file = File.OpenWrite(string.Concat(root, sftpFile.Name))) //path in your local machine
sftp.DownloadFile(sftpFile.FullName, file); } sftp.Disconnect(); }
Other samples are provided in its complete source code and test cases which you can also download.
Configuring a Windows Server to be eligible for SSH/SFTP
I found this link that could assist you to set up your Windows Server to be used for testing your SFTP transactions, using OpenSSH. After following the instructions written there, make sure to restart your server, and make sure that the OpenSSH Server service is running.
If you may encounter this error when trying to start (it usually does not start automatically) the OpenSSH Server service (Could not start the OpenSSH Server service on Local Computer. Error 1067: The process terminated unexpectedly.), you may refer to these (link 1 and link 2) to help you fix the issue.
Using Public and Private keys for SFTP authentication
Referring to the code mentioned above, you may see that there already is a PrivateKeyAuthenticationMethod. It means that to authenticate the file transfer, a public key must be present in the receiving remote server, and a private key is used by the accessing local machine and it should both match before the file transfer should begin.
To assist you in generating a public and private key for your authentication needs, you may use PuttyGen to generate your keys (instructions to do so can also be found here).
Since your server is OpenSSH, private keys may not work because of incompatibility. You may refer to this link to solve your issue.
That's all for now, I hope these links would be able to help you in developing .NET applications with SFTP.
Happy coding!
All the other SFTP libraries that I have seen are way overpriced. This is a good one:
ReplyDeletehttps://www.kellermansoftware.com/p-41-net-sftp-library.aspx