Click here to Skip to main content
13,248,562 members (72,972 online)
Click here to Skip to main content
Add your own
alternative version


130 bookmarked
Posted 10 Dec 2008

An FTP secure client library for C#

, 10 Dec 2008
Rate this:
Please Sign up or sign in to vote.
How to implement an FTP secure connection with an SSL stream class.


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

FTPFactory ftp = new FTPFactory();

//Connect to SSL Port (990)

//Send "AUTH SSL" Command
string cmd = "AUTH SSL";

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.


2. Create SSL Stream

//Create SSL Stream
//Login to FTP Secure

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.


3. Upload File

//Set ASCII Mode

//Send Arguments if you want
//cmd = "site arg1 arg2";

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


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:

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.


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


About the Author

kadaoui el mehdi
Belgium Belgium
in 2005, I started my career. Net and I could improve in this technology through the project WinFroms. Net 2.0 / MVC2 for "Nestle".

After obtaining my diploma MASTER "MBDS" from the University of Nice Sophia Anti-police, I left to Belguim to work as Expert .Net Analyst Developer.

Currently I specialize in architecture to the lowest level.

Meanwhile I remain very active in the community. Net, I created the 1st community. Net Morocco "on Facebook and LinkedIn and twitter, called "Morocco .Net User Group (MONUG)"

You may also be interested in...


Comments and Discussions

GeneralRe: During PASV I get: 500 You need to use a client supporting PRET (PRE Transfer) to use PASV Pin
AndreasLink14-Nov-09 5:23
memberAndreasLink14-Nov-09 5:23 
GeneralCode hangs up on readReply() Pin
Ericc196928-Oct-09 16:57
memberEricc196928-Oct-09 16:57 
GeneralRe: Code hangs up on readReply() Pin
AndreasLink14-Nov-09 5:00
memberAndreasLink14-Nov-09 5:00 
GeneralRe: Code hangs up on readReply() Pin
Member 3728808-Dec-09 10:54
memberMember 3728808-Dec-09 10:54 
GeneralRe: Code hangs up on readReply() Pin
MagicPT13-Jan-10 7:46
memberMagicPT13-Jan-10 7:46 
GeneralRe: Code hangs up on readReply() [modified] Pin
Silve_X18-Jul-10 0:52
memberSilve_X18-Jul-10 0:52 
GeneralError on port=990 Pin
Sameera Homa25-Sep-09 10:37
memberSameera Homa25-Sep-09 10:37 
GeneralRe: Error on port=990 Pin
kadaoui el mehdi25-Sep-09 10:46
memberkadaoui el mehdi25-Sep-09 10:46 
GeneralMessage Closed Pin
25-Sep-09 11:02
memberSameera Homa25-Sep-09 11:02 
GeneralRe: Error on port=990 Pin
kadaoui el mehdi25-Sep-09 11:04
memberkadaoui el mehdi25-Sep-09 11:04 
GeneralRe: Error on port=990 Pin
SonnyDude13-Jul-10 22:13
memberSonnyDude13-Jul-10 22:13 
GeneralRe: Error on port=990 Pin
mendick200016-Nov-10 5:49
membermendick200016-Nov-10 5:49 
GeneralRe: Error on port=990 Pin
maximus.dec.meridius9-Mar-11 6:11
membermaximus.dec.meridius9-Mar-11 6:11 
Answer(Solution) The code hang on the function AuthenticateAsClient of the data stream. Pin
sonnguyen7917-Jul-09 13:48
membersonnguyen7917-Jul-09 13:48 
GeneralRe: (Solution) The code hang on the function AuthenticateAsClient of the data stream. Pin
Kris Munroe22-Sep-09 9:13
memberKris Munroe22-Sep-09 9:13 
AnswerRe: (Solution) The code hang on the function AuthenticateAsClient of the data stream. Pin
Jonathan.wilkins28-Jan-10 22:37
memberJonathan.wilkins28-Jan-10 22:37 
GeneralRe: (Solution) The code hang on the function AuthenticateAsClient of the data stream. Pin
Member 383304120-Oct-09 13:16
memberMember 383304120-Oct-09 13:16 
QuestionUsing the lib to connect a non secure ftp server Pin
david_864015-Jul-09 3:33
memberdavid_864015-Jul-09 3:33 
QuestionFTP Client and Server Pin
aschreiber25-Jun-09 14:10
memberaschreiber25-Jun-09 14:10 
GeneralNotImplementedException Pin
SimpleData6-May-09 3:10
memberSimpleData6-May-09 3:10 
GeneralExplicit mode Pin
adhager21-Apr-09 19:16
memberadhager21-Apr-09 19:16 
GeneralRe: Explicit mode Pin
kadaoui el mehdi5-May-09 4:00
memberkadaoui el mehdi5-May-09 4:00 
GeneralNo welcome message received Pin
optionj8515-Apr-09 2:52
memberoptionj8515-Apr-09 2:52 
GeneralRe: No welcome message received Pin
kadaoui el mehdi17-Apr-09 0:17
memberkadaoui el mehdi17-Apr-09 0:17 
GeneralRe: No welcome message received Pin
optionj8517-Apr-09 2:11
memberoptionj8517-Apr-09 2:11 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 10 Dec 2008
Article Copyright 2008 by kadaoui el mehdi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid