Click here to Skip to main content
11,481,916 members (65,577 online)
Click here to Skip to main content

An FTP secure client library for C#

, 10 Dec 2008 CPOL 224.1K 9.1K 124
Rate this:
Please Sign up or sign in to vote.
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

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)

Share

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

 
QuestionCannot Upload File Pin
luis hernandez11-Aug-11 10:20
memberluis hernandez11-Aug-11 10:20 
AnswerRe: Cannot Upload File Pin
Member 76555695-Apr-12 11:56
memberMember 76555695-Apr-12 11:56 
QuestionError: A non-blocking socket operation could not be completed immediately Pin
bigMoey27-Jun-11 19:29
memberbigMoey27-Jun-11 19:29 
GeneralI can not run this project. Pin
wertyk12-May-11 4:16
memberwertyk12-May-11 4:16 
GeneralRe: I can not run this project. Pin
kadaoui el mehdi12-May-11 7:24
memberkadaoui el mehdi12-May-11 7:24 
GeneralRe: I can not run this project. Pin
wertyk12-May-11 21:25
memberwertyk12-May-11 21:25 
GeneralRe: I can not run this project. Pin
kadaoui el mehdi12-May-11 22:58
memberkadaoui el mehdi12-May-11 22:58 
QuestionError on ftp.loginWihtoutUser Pin
jayson pryde28-Oct-10 12:45
memberjayson pryde28-Oct-10 12:45 
Hi,

I've used your code but getting an exception "An existing connection was forcibly closed by remote host" during ftp,loginWithoutUser. I am sure port 990 is open because I can connect using Filezilla using TLS/SSL with that port.

Any thoughts?

JMPryde
AnswerRe: Error on ftp.loginWihtoutUser Pin
gyingLiow9-Apr-13 21:15
membergyingLiow9-Apr-13 21:15 
GeneralOriginal FTPFactory.cs written by Jaimon Mathew. Pin
JulianPo23-Nov-09 6:40
memberJulianPo23-Nov-09 6:40 
General"An existing connection was forcibly closed by the remote host." [modified] Pin
Hoang dinh Trung18-Nov-09 22:43
memberHoang dinh Trung18-Nov-09 22:43 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
kadaoui el mehdi19-Nov-09 2:06
memberkadaoui el mehdi19-Nov-09 2:06 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
Hoang dinh Trung19-Nov-09 20:37
memberHoang dinh Trung19-Nov-09 20:37 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
kadaoui el mehdi19-Nov-09 21:43
memberkadaoui el mehdi19-Nov-09 21:43 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
Hoang dinh Trung21-Nov-09 5:21
memberHoang dinh Trung21-Nov-09 5:21 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
kadaoui el mehdi24-Nov-09 22:02
memberkadaoui el mehdi24-Nov-09 22:02 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
mdbaskaran30-Dec-09 10:55
membermdbaskaran30-Dec-09 10:55 
GeneralRe: "An existing connection was forcibly closed by the remote host." Pin
SonnyDude13-Jul-10 22:11
memberSonnyDude13-Jul-10 22:11 
GeneralGetting a file listing returning odd characters Pin
hankgrav16-Nov-09 8:40
memberhankgrav16-Nov-09 8:40 
GeneralRe: Getting a file listing returning odd characters Pin
kadaoui el mehdi16-Nov-09 10:20
memberkadaoui el mehdi16-Nov-09 10:20 
GeneralRe: Getting a file listing returning odd characters Pin
hankgrav16-Nov-09 10:26
memberhankgrav16-Nov-09 10:26 
GeneralRe: Getting a file listing returning odd characters Pin
kadaoui el mehdi16-Nov-09 10:47
memberkadaoui el mehdi16-Nov-09 10:47 
GeneralRe: Getting a file listing returning odd characters Pin
hankgrav16-Nov-09 10:56
memberhankgrav16-Nov-09 10:56 
GeneralRe: Getting a file listing returning odd characters Pin
kadaoui el mehdi19-Nov-09 2:11
memberkadaoui el mehdi19-Nov-09 2:11 
GeneralRe: Getting a file listing returning odd characters Pin
Member 1044694020-Jan-14 0:57
memberMember 1044694020-Jan-14 0:57 

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 | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 10 Dec 2008
Article Copyright 2008 by kadaoui el mehdi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid