Solved

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

Posted on 2014-07-21
6
854 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 79

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 79

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Microservice architecture adoption brings many advantages, but can add intricacy. Selecting the right orchestration tool is most important for business specific needs.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

28 Experts available now in Live!

Get 1:1 Help Now