SFTP client in ASP.NET

A basic SFTP client implemented in ASP.NET.

This ASP.NET sample demonstrates the following features:

  • Connecting to an SFTP server from an ASP.NET webpage
  • Persisting SFTP connection info between separate page requests
  • Directory browsing
  • Downloading a file from an SFTP server and sending it through HTTP to the web browser
  • Uploading files through HTTP to ASP.NET page and resending it to SFTP server
  • Creating and deleting directories
  • Resuming downloads

Handling SFTP on ASP.NET

Using SFTP in ASP.NET environment is a bit tricky due to a fundamental difference in SFTP and HTTP protocols. The ASP.NET server communicates with the web browser via HTTP (stateless) protocol while SFTP is stateful, session oriented protocol.

Web browser simply connects to the ASP.NET server, fetches a page or other resources and disconnects. Basically, each request/response pair uses a separate connection to the web server, and although HTTP 1.1 makes it possible to reuse a connection for more than one request, the two request/response pairs are pretty much independent.

The SFTP client behaves differently. It connects at the beginning, establishing a session. It then uploads or downloads multiple files, creates directories and disconnects at the end - all during the single connection to the SFTP server.

When using the SFTP protocol from an ASP.NET page, we have two ways of handling it:

  • Use SFTP as stateless protocol.

    Connect to SFTP, do what needs to be done at the moment and disconnect on each ASP.NET page request. Slower, but safe.
  • Try to maintain SFTP session between separate ASP.NET page requests.

    Faster, but brings many problems - e.g.: How to handle users who open dozens of SFTP-enabled pages in their browser and never close them? How to store the connected instance of SFTP object? Into an ASP.NET Session? What about concurrent access then? And what if the session is persisted into a database? How to handle web farms?

This sample implements the first approach - connect and disconnect during each ASP.NET page request. The user has to enter SFTP server name, username and password on first access. This connection info is then stored for later use in the ASP.NET session state.

Uploading a file (Browser -> ASP.NET Server -> SFTP Server)

Communication scheme:

  1. Web browser connects to ASP.NET web server and starts sending the file selected using the FileUpload control.
  2. ASP.NET application (at the web server) connects to the SFTP server.
  3. ASP.NET application starts receiving the file from the web browser. All received data is immediately sent to the SFTP server.
  4. ASP.NET application closes the connection to the SFTP server.

Downloading a file (SFTP Server -> ASP.NET Server -> Browser)

Communication scheme:

  1. Web browser connects to ASP.NET web server and requests the file download.
  2. ASP.NET application (at the web server) connects to SFTP server and requests the file download.
  3. SFTP server starts sending the file back to the ASP.NET application.
  4. ASP.NET application starts receiving the file. All received data is immediately sent to the web browser on the client machine.
  5. ASP.NET application closes the connection to the SFTP server.

Included in

See also: