Wednesday, 11 June 2014

SFTP upload with .Net Application


Today I was working on a utility which need to upload data on FTP. I have implemented code to upload on FTP using FTP web request. But end of the day when I finally deployed the code, I received the details of SFTP account with host, username, password and port. When I configured these details in utility the utility has failed to upload file on FTP.

Then I started researching reason of failure and found that .Net don’t support secured FTP i. e. SFTP. After searching over net I found that we need to use third party library to upload files on SFTP while using with .Net. There are many third party libraries available for SFTP, two of them are given below that could be used for this purpose.

         1.      Rebex SFTP
         2.      SharpSSH

SharpSSH is open source library and published with BSD License. I have decided to use this one for SFTP functionality implementation.

SharpSSH is a pure .NET implementation of the SSH2 client protocol suite. It provides an API for communication with SSH servers and could be integrated into any .NET application.

I am just going to discuss quick integration of this library to implement SFTP functionality in your application. You can download source code or DLLs for SharpSSH here.

Sharp SSH is actually using some java library and commands to support SFTP which is covered in a wrapper to work with .Net.

You will get following three DLLs in download
          1.      Tamir.SharpSSH.dll
          2.      Org.Mentalis.Security.dll
          3.      DiffieHellman.dll

You need to add reference to Tamir.SharpSSH while two other DLLs should also be referenced or added in bin. Org.Mentalis.Security dll is used for encryption. Now let’s come directly to the small piece of code which is doing SFTP here.

//Include namespace for Tamir.SharpSSH and System.IO as below.
using Tamir.SharpSsh;
using System.IO;

//Collect SFTP account details and root directory to upload file in.
string m_FTPServerAddress = "x.x.x.x"; // this is host IP or host domain name
string m_FTPUser = "username"; // this is username as needed to access SFTP
string m_FTPPassword = "Password"; // this is password as needed to access SFTP
string m_FTPPort = "Port"; //this is port number which is configured in SFTP for communucation.

string m_RemoteFTPFolder = "/RootFolderName/"; // this is root folder name as configured in SFTP.

string filePathAndName = "File path"; //This is cmplete file name with physical path.

//Create object for SFTP class as in Tamir.SharpSSH dll.
Sftp scp = new Sftp(m_FTPServerAddress, m_FTPUser, m_FTPPassword);

//Connect to SFTP using port as provided.
scp.Connect(m_FTPPort.ToIntSafe());

//Put/upload file on SFTP from source location to destination location in SFTP root directory.
scp.Put(filePathAndName, m_RemoteFTPFolder + Path.GetFileName(filePathAndName));

//Close connection from SFTP after uploading.
scp.Close();

Now refer above code and notice three magical lines which are doing actual job here as below.
1.      Following line of code creates an object of a SFTP class as in Tamir.SharpSSH using available ftp server address which is host ip/name, SFTP user name and SFTP password.

Sftp scp = new Sftp(m_FTPServerAddress, m_FTPUser, m_FTPPassword);

2.      Following line connects and creates a communication channel between application and SFTP host using specific post as provided in “m_FTPPort” variable.

scp.Connect(m_FTPPort.ToIntSafe());

3.      Following line of code uploads file from source path to destination path on Sftp root.

//scp.Put(source_file, destination_file);
scp.Put(filePathAndName, m_RemoteFTPFolder + Path.GetFileName(filePathAndName));

4.      Following line of code finally closes connection and communication channel from SFTP host.

scp.Close();

The code as explained above is tested and working properly. While I was trying to implement this code I came with an exception with no information in it. But after researching and trying I come to know that I was giving just file name in destination file as specified in point 3. While root folder of SFTP was needed as prefixed with file name to successfully upload file on SFTP server.

I just mentioned above point so that no one other should stuck in such a small problem.
Now enjoy SFTP upload just like normal FTP upload.

2 comments:

  1. Dear sir ,

    after using your code , i have face below error.

    Session.connect: System.IO.FileLoadException: Could not load file or assembly 'DiffieHellman, Version=0.0.0.0, Culture=neutral, PublicKeyToken=893fce744c3d0315' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.

    please give me solution.

    thanks and regards ,

    Ronak Patel

    ReplyDelete
  2. Sorry for delayed response Rony,
    You may need to download latest set of all DLLs as per your development environment. i. e. 32 or 64 bit machine, .net or Java version etc.

    ReplyDelete