WebRequest using SSL
Where do you validate the SSL certificate? Doing SSL over an FTP connection isn't quite as simple as setting the .EnableSsl
property. You need to provide a certificate validation method. See this article for the C# code to do what you want. Also, someone copied and pasted their whole FTP class in this MSDN article if you need a more detailed implementation.
Just to quickly get you up and running quickly, test with this:
if (request.EnableSsl) ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
and then later:
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
return true; // Read the links provided above for real implementation
}
Paul Michaels
I've been a programmer for most of my life. I have an interest in games, mobile and tablet development, along with message queuing, and pretty much anything that provides an elegant solution to a problem, technical or otherwise. I like learning new technology and finding new ways to use the old. I blog about my experiences here. You can read about me, or contact me on Linked in here.
Updated on June 27, 2022Comments
-
Paul Michaels almost 2 years
I have the following code to retrieve a file using FTP (which works fine).
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(svrPath); request.KeepAlive = true; request.UsePassive = true; request.UseBinary = true; request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential(uname, passw); using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) using (Stream responseStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream)) using (StreamWriter destination = new StreamWriter(destinationFile)) { destination.Write(reader.ReadToEnd()); destination.Flush(); }
However, when I try to do this using SSL, I am unable to access the file, as follows:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(svrPath); request.KeepAlive = true; request.UsePassive = true; request.UseBinary = true; // The following line causes the download to fail request.EnableSsl = true; request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential(uname, passw); using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) using (Stream responseStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream)) using (StreamWriter destination = new StreamWriter(destinationFile)) { destination.Write(reader.ReadToEnd()); destination.Flush(); }
Can anyone tell me why the latter would not work?
EDIT:
I get the following exception:
The remote server returned an error: (530) Not logged in.
-
Daniel Renshaw almost 14 yearsWhat is the error number/message?
-
Paul Michaels almost 14 yearsSorry - I've edited my question
-
Prix almost 14 yearsHave you tried connecting to the ftp using ssl with any other application to confirm it has ssl enabled ?
-
-
Jeremy Holovacs over 5 yearsany way to do this on a case-by-case basis? That seems to be global.