WebRequest using SSL

I have the following code to extract 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 cannot access the file as shown below:

  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 someone tell me why the latter is not working?

EDIT:

I get the following exception:

 The remote server returned an error: (530) Not logged in. 
+4
source share
2 answers

Where do you confirm the SSL certificate? Performing SSL over an FTP connection is not as simple as setting the .EnableSsl property. You need to provide a certificate verification method. See this article for C # code to do what you want. In addition, someone copied and pasted their entire FTP class into this MSDN article if you need a more detailed implementation.

To get you up and running quickly, consult 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 } 
+8
source

Try this FtpWebRequest request = (FtpWebRequest) FtpWebRequest.Create (svrPath);

-3
source

Source: https://habr.com/ru/post/1312845/


All Articles