I saw this post giving the simple idea of uploading files to ftp by using WebClient
. This is simple, but how do I force it to use SSL?
Asked
Active
Viewed 6,546 times
5
-
1Possible duplicate of [FTPS (FTP over SSL) in C#](https://stackoverflow.com/questions/4331665/ftps-ftp-over-ssl-in-c-sharp) – Sphinxxx Jan 16 '18 at 00:17
1 Answers
6
The answer here by Edward Brey
might answer your question. Instead of providing my own answer I will just copy what Edward says:
You can use FtpWebRequest; however, this is fairly low level. There is a higher-level class WebClient, which requires much less code for many scenarios; however, it doesn't support FTP/SSL by default. Fortunately, you can make WebClient work with FTP/SSL by registering your own prefix:
private void RegisterFtps()
{
WebRequest.RegisterPrefix("ftps", new FtpsWebRequestCreator());
}
private sealed class FtpsWebRequestCreator : IWebRequestCreate
{
public WebRequest Create(Uri uri)
{
FtpWebRequest webRequest = (FtpWebRequest)WebRequest.Create(uri.AbsoluteUri.Remove(3, 1)); // Removes the "s" in "ftps://".
webRequest.EnableSsl = true;
return webRequest;
}
}
Once you do this, you can use WebRequest almost like normal, except that your URIs start with "ftps://" instead of "ftp://". The one caveat is that you have to specify the method, since there won't be a default one. E.g.
// Note here that the second parameter can't be null.
webClient.UploadFileAsync(uploadUri, WebRequestMethods.Ftp.UploadFile, fileName, state);

Community
- 1
- 1

Thorsten Dittmar
- 55,956
- 8
- 91
- 139
-
-
This works for FTPS, but *FTPS* (FTP+SSL) is a rare animal. If you're trying to connect to an *SFTP* server (file transfer over SSH), which is a much more common scenario, WebClient can't do that. You'd have to look at a separate library such as SSH.NET. – bobince Jul 15 '15 at 20:18
-
@bobince a) Thanks. But FTP+SSL is what I want. b) I don't get notified of your comment unless you add the `@MyName`. Thanks again. – ispiro Jul 15 '15 at 20:25
-
but does this need to validate any certificates , or are all auto-accepted ? – Ahmed Fwela Jul 21 '16 at 15:06
-
1I have not come up with this myself. I suggest you follow the linked answer and ask the original poster himself. My first guess would be that you need to implement something to accept invalid certificates. Something along the lines of `ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;` – Thorsten Dittmar Jul 21 '16 at 15:12
-
1@bigworld12 When you `EnableSsl`, by default the client validates certificates using the OS's public key infrastructure. Be aware that if you bypass validation by blindly accepting any server certificate, you become vulnerable to a man-in-the-middle attack. – Edward Brey Jan 16 '18 at 00:31