Solved

Cannot upload files to FTP site using SSL encryption from C# code

Posted on 2014-07-21
6
828 Views
Last Modified: 2014-07-23
I'm trying to write C# code (.NET 4.0) to upload files using FTP with a site that requires SSL encrypted communications.

Here's my code:

static void FTPUpload(string filename)
        {

            string ftpSite = (string)System.Configuration.ConfigurationManager.AppSettings["ftpSite"];
            string ftpUserName = (string)System.Configuration.ConfigurationManager.AppSettings["ftpUserName"];
            string ftpPassword = (string)System.Configuration.ConfigurationManager.AppSettings["ftpPassword"];
            string ftpEnableSSL = (string)System.Configuration.ConfigurationManager.AppSettings["EnableSSL"];

            FileInfo fileInf = new FileInfo(filename);
            string uri = "ftp://" + ftpSite + "/" + fileInf.Name;
            FtpWebRequest reqFTP;
   
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpSite + "/" + fileInf.Name));

            reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

            if (ftpEnableSSL == "true")
            {
                reqFTP.EnableSsl = true;

                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
// see function AcceptAllCertifications below
            }

            if (ftpEnableSSL == "false") reqFTP.EnableSsl = false;
   
            reqFTP.KeepAlive = true;

            reqFTP.UsePassive = true;

            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
   
            reqFTP.UseBinary = true;

            reqFTP.ContentLength = fileInf.Length;

            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
   
            FileStream fs = fileInf.OpenRead();
   
            Stream strm = reqFTP.GetRequestStream();
       
            contentLen = fs.Read(buff, 0, buffLength);
       
            while (contentLen != 0)
            {
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
            }
            strm.Close();
            fs.Close();
        }


        static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }

When this code runs, I get the exception message "Unable to connect to the remote server".

The line throwing the exception is Stream strm = reqFTP.GetRequestStream();

When I use this code with an FTP site (outside my network) that does not use SSL encryption, the files I create are uploaded.

When I use Filezilla to upload files to the site requiring SSL, I can upload files when I set Protocol to SFTP. Also, when I set up the site in Filezilla, I copied the user name and password out of my config file.

I need some kind of answer by 8/26. The ideal answer would be a change to my code that will allow the upload to work. The second best answer would be a free DLL that can be called from my code that can do the upload. The third choice would be a commercial DLL that can be called from my code.

Thanks for your help.
0
Comment
Question by:CRESLISA
  • 4
  • 2
6 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 40211201
are you sure that it is ftp/s and not sftp, ftp/s uses certificates and sftp (SSH File Transfer Protocol, a network protocol for secure file transfer over secure shell ) source:wikipedia
0
 
LVL 1

Author Comment

by:CRESLISA
ID: 40211906
I don't know for sure what the site uses. But I'm able to upload using Filezilla with the sftp setting, and that's the only secure setting in that program.

Since you brought up this issue, please supply code for both methods. I assume you would not respond to this question unless you know C# code that accomplish what I'm trying to do.
0
 
LVL 78

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
ID: 40213497
They are entirely different protocols and S/FTP is not natively in .net you will need a 3rd party library like:
http://www.routrek.co.jp/en/product/varaterm/granados.html
http://www.tamirgal.com/blog/page/SharpSSH.aspx
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:CRESLISA
ID: 40215585
This looks like the answer I need. I ran the example code with the Sharp demo and was able to upload a file. But I need to use a component in my C# program. If you have an example of how to use this in a C# program, that would be greatly appreciated.
0
 
LVL 1

Author Comment

by:CRESLISA
ID: 40215622
OK, it was easy to use. Thanks! And it worked!
0
 
LVL 1

Author Closing Comment

by:CRESLISA
ID: 40215627
The B is because code was not supplied. But it was easy to figure out code that worked. And, since there is no A- or B+, I'm giving a B.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now