?
Solved

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

Posted on 2014-07-21
6
Medium Priority
?
1,044 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 82

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 82

Accepted Solution

by:
David Johnson, CD, MVP earned 1500 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
Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

 
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

Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

765 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