Click here to Skip to main content
Click here to Skip to main content

An FTP secure client library for C#

By , 10 Dec 2008
 

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

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

//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

//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:

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)

About the Author

kadaoui el mehdi
Architect
Belgium Belgium
Member
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 Asp.net 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)"

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionsftp PinmemberKing Ravi Kumar14 May '13 - 2:13 
QuestionDoes anyone have an implemenation example ? PinmemberMember 979519322 Mar '13 - 8:24 
QuestionAn unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call PinmemberMember 982113318 Feb '13 - 8:26 
QuestionHow to run this program. PinmemberMember 982113318 Feb '13 - 8:05 
QuestionFTPS - Connecting to the server through proxy PinmemberMember 251419017 Jan '13 - 21:19 
QuestionWhy is the SslStream object stuff commented out? Is this truly FTP over SSL without that? Pinmemberandegre16 Aug '12 - 8:31 
QuestionDir not working with secure PinmemberBig D30 May '12 - 7:47 
AnswerAre you having issues uploading and downloading files? PinmemberMember 76555695 Apr '12 - 11:09 
QuestionIndex and length must refer to a location within the string.\r\nParameter name: length PinmemberMember 84911326 Mar '12 - 21:56 
QuestionNo port 990 PinmemberMember 855570311 Jan '12 - 7:02 
Questionneeds your helps PinmemberJacob P Yamarthi18 Oct '11 - 21:53 
QuestionUpload File - Nothing Happens PinmemberSergio Marchetti22 Aug '11 - 12:04 
QuestionCannot Upload File Pinmemberluis hernandez11 Aug '11 - 9:20 
QuestionError: A non-blocking socket operation could not be completed immediately PinmemberbigMoey27 Jun '11 - 18:29 
GeneralI can not run this project. Pinmemberwertyk12 May '11 - 3:16 
QuestionError on ftp.loginWihtoutUser Pinmemberjayson pryde28 Oct '10 - 11:45 
GeneralOriginal FTPFactory.cs written by Jaimon Mathew. PinmemberJulianPo23 Nov '09 - 5:40 
General"An existing connection was forcibly closed by the remote host." [modified] PinmemberHoang dinh Trung18 Nov '09 - 21:43 
GeneralGetting a file listing returning odd characters Pinmemberhankgrav16 Nov '09 - 7:40 
GeneralDuring PASV I get: 500 You need to use a client supporting PRET (PRE Transfer) to use PASV PinmemberAndreasLink14 Nov '09 - 3:56 
GeneralCode hangs up on readReply() PinmemberEricc196928 Oct '09 - 15:57 
GeneralError on port=990 PinmemberSameera Homa25 Sep '09 - 9:37 
Answer(Solution) The code hang on the function AuthenticateAsClient of the data stream. Pinmembersonnguyen7917 Jul '09 - 12:48 
QuestionUsing the lib to connect a non secure ftp server Pinmemberdavid_864015 Jul '09 - 2:33 
QuestionFTP Client and Server Pinmemberaschreiber25 Jun '09 - 13:10 
GeneralNotImplementedException PinmemberSimpleData6 May '09 - 2:10 
GeneralExplicit mode Pinmemberadhager21 Apr '09 - 18:16 
GeneralNo welcome message received Pinmemberoptionj8515 Apr '09 - 1:52 
QuestionCert invalid? Pinmemberhk_willow3 Mar '09 - 11:47 
GeneralIndex Out of bounds Pinmemberhk_willow23 Feb '09 - 12:49 
QuestionThe requested name is valid, but no data of the requested type was found Pinmemberdsac11 Feb '09 - 6:10 
QuestiondownlodSecure? PinmemberJohnDoe177 Feb '09 - 6:48 
Generalprivate key issue Pinmemberflemingqin21 Jan '09 - 6:06 
GeneralWorks like a charm PinmemberTSNY20 Jan '09 - 5:04 
GeneralHangs PinmemberMember 349382715 Dec '08 - 10:20 

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

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