Click here to Skip to main content
15,176,823 members
Articles / Programming Languages / C#
Posted 10 Dec 2008

Tagged as


131 bookmarked

An FTP secure client library for C#

Rate me:
Please Sign up or sign in to vote.
4.60/5 (33 votes)
10 Dec 2008CPOL2 min read
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
No Biography provided

Comments and Discussions

QuestionExplicit FTP over TLS Pin
SoftEnd9-Jun-16 22:20
professionalSoftEnd9-Jun-16 22:20 
Question_sslStream.AuthenticateAsClient hangout Pin
Salar IB14-Mar-15 8:07
MemberSalar IB14-Mar-15 8:07 
AnswerRe: _sslStream.AuthenticateAsClient hangout Pin
ravit2326-Mar-18 1:22
Memberravit2326-Mar-18 1:22 
QuestionException Pin
MehraRaj25-Dec-14 19:01
MemberMehraRaj25-Dec-14 19:01 
GeneralBeware and use with caution Pin
Seva Zaslavsky11-Jul-14 3:03
MemberSeva Zaslavsky11-Jul-14 3:03 
GeneralRe: Beware and use with caution Pin
Antonio Ripa1-Aug-14 12:05
professionalAntonio Ripa1-Aug-14 12:05 
SuggestionAnother library to try if you have problems with this one.... Pin
gmagana29-Jan-14 14:37
Membergmagana29-Jan-14 14:37 
QuestionAplication freezing after login method Pin
Leo Fraietta14-Jan-14 9:45
MemberLeo Fraietta14-Jan-14 9:45 
Questionsftp Pin
King Ravi Kumar 14-May-13 3:13
MemberKing Ravi Kumar 14-May-13 3:13 
QuestionDoes anyone have an implemenation example ? Pin
Member 979519322-Mar-13 9:24
MemberMember 979519322-Mar-13 9:24 
QuestionAn unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call Pin
Member 982113318-Feb-13 9:26
MemberMember 982113318-Feb-13 9:26 
QuestionHow to run this program. Pin
Member 982113318-Feb-13 9:05
MemberMember 982113318-Feb-13 9:05 
QuestionFTPS - Connecting to the server through proxy Pin
Member 251419017-Jan-13 22:19
MemberMember 251419017-Jan-13 22:19 
QuestionWhy is the SslStream object stuff commented out? Is this truly FTP over SSL without that? Pin
andegre16-Aug-12 9:31
Memberandegre16-Aug-12 9:31 
QuestionDir not working with secure Pin
eyedia30-May-12 8:47
Membereyedia30-May-12 8:47 
AnswerAre you having issues uploading and downloading files? Pin
Member 76555695-Apr-12 12:09
MemberMember 76555695-Apr-12 12:09 
GeneralRe: Are you having issues uploading and downloading files? Pin
gmagana29-Jan-14 12:26
Membergmagana29-Jan-14 12:26 
QuestionIndex and length must refer to a location within the string.\r\nParameter name: length Pin
Member 84911326-Mar-12 22:56
MemberMember 84911326-Mar-12 22:56 
QuestionNo port 990 Pin
Member 855570311-Jan-12 8:02
MemberMember 855570311-Jan-12 8:02 
AnswerRe: No port 990 Pin
bob mourning2-Mar-12 3:49
Memberbob mourning2-Mar-12 3:49 
Questionneeds your helps Pin
Jacob P Yamarthi18-Oct-11 22:53
MemberJacob P Yamarthi18-Oct-11 22:53 
AnswerRe: needs your helps Pin
Member 76555695-Apr-12 11:57
MemberMember 76555695-Apr-12 11:57 
GeneralRe: needs your helps Pin
volax12-Feb-13 6:39
Membervolax12-Feb-13 6:39 
QuestionUpload File - Nothing Happens Pin
Sergio Marchetti22-Aug-11 13:04
MemberSergio Marchetti22-Aug-11 13:04 
AnswerRe: Upload File - Nothing Happens Pin
Member 76555695-Apr-12 11:56
MemberMember 76555695-Apr-12 11:56 

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.