0

I'm trying to send a file to a ftp server using ftps in C#. I am using filezilla server to test locally. I already tried using just regular ftp (without EnableSsl) and it works fine. I've already looked around and I'm trying to set up the certificates, but I can't get it to work. I keep getting error: the remote certificate is invalid according to the validation procedure. On filezilla, I used their "generate certificate" in the settings and saved it to desktop. So what is the file I need to use to create certificate from? Is it the one filezilla generated? If so, its a .crt file, so I wasn't able to use the CreateFromCertFile() method..

This is where I add the certificate

X509Certificate cert = X509Certificate.CreateFromCertFile(pathtofile);
  X509CertificateCollection certCollection = new X509CertificateCollection();
  certCollection.Add(cert);

  request.ClientCertificates.Add(cert);

I have the ftp request like this

 var request = (FtpWebRequest) WebRequest.Create(fullPath);
      request.UseBinary = true;
      request.Method = WebRequestMethods.Ftp.UploadFile;
      request.EnableSsl = true;
 request.KeepAlive = false;
      request.Credentials = new NetworkCredential(username, password);
      request.UsePassive = true;

Thanks in advance

1 Answers1

1

If your certificate is self-signed, you need to tell ServicePointManager to accept it.

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  • 1
    You may want to refer to [this other answer](http://stackoverflow.com/questions/526711/using-a-self-signed-certificate-with-nets-httpwebrequest-response) – Jorge Yanes Diez Feb 26 '14 at 22:17
  • Add the cert to the local store. Don't override cert validation – Jon Barker Feb 27 '14 at 01:25
  • @JonBarker Your approach is more formally correct, but my intention was just to show that the certificate was being rejected due to the fact it is self-signed. I should have made it clearer this is not a solution for production code, but that is why I added the comment to look at the other answer, which already makes that clear. – Jorge Yanes Diez Feb 27 '14 at 08:51