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
Rate this:
Please Sign up or sign in to vote.

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
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)"

Comments and Discussions

 
GeneralRe: Getting a file listing returning odd characters Pinmemberhankgrav16-Nov-09 9:26 
GeneralRe: Getting a file listing returning odd characters Pinmemberkadaoui el mehdi16-Nov-09 9:47 
GeneralRe: Getting a file listing returning odd characters Pinmemberhankgrav16-Nov-09 9:56 
GeneralRe: Getting a file listing returning odd characters Pinmemberkadaoui el mehdi19-Nov-09 1:11 
GeneralRe: Getting a file listing returning odd characters PinmemberMember 1044694019-Jan-14 23:57 
GeneralDuring PASV I get: 500 You need to use a client supporting PRET (PRE Transfer) to use PASV PinmemberAndreasLink14-Nov-09 3:56 
GeneralRe: During PASV I get: 500 You need to use a client supporting PRET (PRE Transfer) to use PASV PinmemberAndreasLink14-Nov-09 4:23 
GeneralCode hangs up on readReply() PinmemberEricc196928-Oct-09 15:57 
I am new to FTPS, so this library looked like it would save me a lot of time. I am watching the code execute, and my app hangs everytime the readReply() function is called. I also notice some commented out code for SslStream in the loginWithoutUser() function. THe line is //SslStream sslStream = new SslStream(client.GetStream(), false);
 
Is this line needed? Does anyone know why I am getting a hang when I call the loginWithoutUser (at the readReply function).
 
My code looks as follows:
 
FTP_DLL.FTPFactory factory = new FTP_DLL.FTPFactory();
 
factory.setDebug(true);
factory.setRemoteHost("hostname");
 
//Connect to SSL Port (990)
factory.setRemotePort(990);
factory.loginWithoutUser();
 
//Send "AUTH SSL" Command
string cmd = "AUTH SSL";
factory.sendCommand(cmd);
 
//Create SSL Stream
factory.getSslStream();
factory.setUseStream(true);
//Login to FTP Secure
factory.setRemoteUser("user");
factory.setRemotePass("pass");
factory.login();
 
Any help would be greatly appreciated. I am wokring on a deadline for figuring this out, so the sooner the better!
 
Thanks
GeneralRe: Code hangs up on readReply() PinmemberAndreasLink14-Nov-09 4:00 
GeneralRe: Code hangs up on readReply() PinmemberMember 3728808-Dec-09 9:54 
GeneralRe: Code hangs up on readReply() PinmemberMagicPT13-Jan-10 6:46 
GeneralRe: Code hangs up on readReply() [modified] PinmemberSilve_X17-Jul-10 23:52 
GeneralError on port=990 PinmemberSameera Homa25-Sep-09 9:37 
GeneralRe: Error on port=990 Pinmemberkadaoui el mehdi25-Sep-09 9:46 
GeneralRe: Error on port=990 PinmemberSameera Homa25-Sep-09 10:02 
GeneralRe: Error on port=990 Pinmemberkadaoui el mehdi25-Sep-09 10:04 
GeneralRe: Error on port=990 PinmemberSonnyDude13-Jul-10 21:13 
GeneralRe: Error on port=990 Pinmembermendick200016-Nov-10 4:49 
GeneralRe: Error on port=990 Pinmembermaximus.dec.meridius9-Mar-11 5:11 
Answer(Solution) The code hang on the function AuthenticateAsClient of the data stream. Pinmembersonnguyen7917-Jul-09 12:48 
GeneralRe: (Solution) The code hang on the function AuthenticateAsClient of the data stream. PinmemberKris Munroe22-Sep-09 8:13 
AnswerRe: (Solution) The code hang on the function AuthenticateAsClient of the data stream. PinmemberJonathan.wilkins28-Jan-10 21:37 
GeneralRe: (Solution) The code hang on the function AuthenticateAsClient of the data stream. PinmemberMember 383304120-Oct-09 12:16 
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 

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

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

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