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
Solved

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

Posted on 2014-07-21
6
894 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 80

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 80

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Microservice architecture adoption brings many advantages, but can add intricacy. Selecting the right orchestration tool is most important for business specific needs.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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