Solved

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

Posted on 2014-07-21
6
982 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 81

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 81

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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