Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Article

An FTP secure client library for C#

Rate me:
Please Sign up or sign in to vote.
4.61/5 (34 votes)
10 Dec 2008CPOL2 min read 491.3K   15.8K   135   106
How to implement an FTP secure connection with an SSL stream class.

Introduction

The purpose of this article is to create a C # FTP client in Secure mode, so if you don’t have much knowledge of FTPS, I advise you to take a look at this: FTPS.

In the .NET Framework, to upload a file in FTPS mode, we generally use the FtpWebRequest class, but you can not send commands with « quote » arguments, and even if you search on the web, you will not find a concrete example of a secured C# FTP client.

It’s for those reasons I decided to create this article.

SSL Stream?

To send a socket in Secure Socket Layer (SSL) mode, we use the class System.Net.Security.SslStream.

Provides a stream used for client-server communication that uses the Secure Socket Layer (SSL) security protocol to authenticate the server and optionally the client”.

For more information, refer to MSDN.

Using the Code

1. Pre-Authenticate

C#
FTPFactory ftp = new FTPFactory();
ftp.setDebug(true);
ftp.setRemoteHost(Settings.Default.TargetFtpSource);

//Connect to SSL Port (990)
ftp.setRemotePort(990);
ftp.loginWithoutUser();

//Send "AUTH SSL" Command
string cmd = "AUTH SSL";
ftp.sendCommand(cmd);

Before connecting to the FTP server in SSL mode, you have to define the 990 SSL port and send the « AUTH SSL » command authentication using SSL.

WelcomeFTPSecure.JPG

2. Create SSL Stream

C#
//Create SSL Stream
ftp.getSslStream();
ftp.setUseStream(true);
//Login to FTP Secure
ftp.setRemoteUser(Settings.Default.TargetFtpSecureUser);
ftp.setRemotePass(Settings.Default.TargetFtpSecurePass);
ftp.login();

ftp.getSslStream() creates an SSL stream from client socket which will be used for exchange between the client and the server FTPS. Then, you have to enter a login and password to authenticate on the FTPS server.

Note: if the SSL stream is well established, then I display all the information about the server certificate.

CertificatInfos.JPG

3. Upload File

C#
//Set ASCII Mode
ftp.setBinaryMode(false);

//Send Arguments if you want
//cmd = "site arg1 arg2";
//ftp.sendCommand(cmd);

//Upload file
ftp.uploadSecure(@"Filepath", false);

ftp.close();

Before uploading a file, you have to specify the mode: ftp.setBinaryMode(bool value).

  • value is false --> ASCII mode
  • value is true --> Binary mode

By default, it’s binary. Now, you upload the file using:

C#
ftp.uploadSecure(@"Filepath", false)

Note: you can upload in non secured mode using ftp.upload().

Points of Interest

I learned how an SSL stream and the RAW FTP communication works between a client and an FTP server. Searching on the web, I found many who were stuck on the issue of SSL communication with an FTP server, so I hope this article will be of great help.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General"An existing connection was forcibly closed by the remote host." [modified] Pin
Hoang dinh Trung18-Nov-09 21:43
Hoang dinh Trung18-Nov-09 21:43 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
kadaoui el mehdi19-Nov-09 1:06
kadaoui el mehdi19-Nov-09 1:06 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
Hoang dinh Trung19-Nov-09 19:37
Hoang dinh Trung19-Nov-09 19:37 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
kadaoui el mehdi19-Nov-09 20:43
kadaoui el mehdi19-Nov-09 20:43 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
Hoang dinh Trung21-Nov-09 4:21
Hoang dinh Trung21-Nov-09 4:21 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
kadaoui el mehdi24-Nov-09 21:02
kadaoui el mehdi24-Nov-09 21:02 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
mdbaskaran30-Dec-09 9:55
mdbaskaran30-Dec-09 9:55 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
SonnyDude13-Jul-10 21:11
SonnyDude13-Jul-10 21:11 
GeneralGetting a file listing returning odd characters Pin
hankgrav16-Nov-09 7:40
hankgrav16-Nov-09 7:40 
I'm trying to get a file listing from a folder, but it's returning garbage, and I can't figure out what's going on.

Here is the code that I am using. I've tried NLST, LIST, and LS, none of which have worked.

public string[] getFileList(string mask)
    {

      if(!logined)
      {
        login();
      }

      Socket cSocket = createDataSocket();
      this.getSslDataStream(cSocket);
      sendCommand("NLST " + mask);

      stream2.AuthenticateAsClient(remoteHost,
           null,
           System.Security.Authentication.SslProtocols.Ssl3 |
           System.Security.Authentication.SslProtocols.Tls,
           true);

      if(!(retValue == 150 || retValue == 125))
      {
        throw new IOException(reply.Substring(4));
      }
      StringBuilder mes = new StringBuilder();

      while(true)
      {

        int bytes = cSocket.Receive(buffer, buffer.Length, 0);
        mes.Append(ASCII.GetString(buffer, 0, bytes));

        if(bytes < buffer.Length)
        {
          break;
        }
      }


Here are the responses that I am getting from the server.

READ:250 Directory changed to /d:/FTP_Transfer

 WRITE:PASV

 READ:227 Entering Passive Mode (10,0,2,24,5,102)

 WRITE:NLST Edeposit

 READ:150 Opening ASCII mode data connection for /bin/ls.

Server Certificate Issued To: CN=ftp.*.com
Server Certificate Issued By: CN=ftp.*.com
Server Certificate Validation Error
RemoteCertificateChainErrors
 READ:226 Transfer complete.

 WRITE:QUIT

 READ:221 Goodbye!


The method returns this:


?7BC??f?i?#?,?????h?P?O?????3?g???????q?14	?l
???#?U0#????B????.????Ku?8??y???k


Any ideas as to what's going on?

Thanks in advance.

Aaron
GeneralRe: Getting a file listing returning odd characters Pin
kadaoui el mehdi16-Nov-09 9:20
kadaoui el mehdi16-Nov-09 9:20 
GeneralRe: Getting a file listing returning odd characters Pin
hankgrav16-Nov-09 9:26
hankgrav16-Nov-09 9:26 
GeneralRe: Getting a file listing returning odd characters Pin
kadaoui el mehdi16-Nov-09 9:47
kadaoui el mehdi16-Nov-09 9:47 
GeneralRe: Getting a file listing returning odd characters Pin
hankgrav16-Nov-09 9:56
hankgrav16-Nov-09 9:56 
GeneralRe: Getting a file listing returning odd characters Pin
kadaoui el mehdi19-Nov-09 1:11
kadaoui el mehdi19-Nov-09 1:11 
GeneralRe: Getting a file listing returning odd characters Pin
Member 1044694019-Jan-14 23:57
Member 1044694019-Jan-14 23:57 
GeneralDuring PASV I get: 500 You need to use a client supporting PRET (PRE Transfer) to use PASV Pin
AndreasLink14-Nov-09 3:56
AndreasLink14-Nov-09 3:56 
GeneralRe: During PASV I get: 500 You need to use a client supporting PRET (PRE Transfer) to use PASV Pin
AndreasLink14-Nov-09 4:23
AndreasLink14-Nov-09 4:23 
GeneralCode hangs up on readReply() Pin
Ericc196928-Oct-09 15:57
Ericc196928-Oct-09 15:57 
GeneralRe: Code hangs up on readReply() Pin
AndreasLink14-Nov-09 4:00
AndreasLink14-Nov-09 4:00 
GeneralRe: Code hangs up on readReply() Pin
Member 3728808-Dec-09 9:54
Member 3728808-Dec-09 9:54 
GeneralRe: Code hangs up on readReply() Pin
MagicPT13-Jan-10 6:46
MagicPT13-Jan-10 6:46 
GeneralRe: Code hangs up on readReply() [modified] Pin
Silve_X17-Jul-10 23:52
Silve_X17-Jul-10 23:52 
GeneralError on port=990 Pin
ZoyaKhan1825-Sep-09 9:37
ZoyaKhan1825-Sep-09 9:37 
GeneralRe: Error on port=990 Pin
kadaoui el mehdi25-Sep-09 9:46
kadaoui el mehdi25-Sep-09 9:46 
GeneralMessage Closed Pin
25-Sep-09 10:02
ZoyaKhan1825-Sep-09 10:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.