Use SSL with FTP (FTPS)
Using FTPS (FTP protocol over secure SSL channel is easy with Ftp.dll .NET FTPS component. The only difference compared to the FTP protocol is that you need to use ConnectSSL method instead of regular Connect:
// C# version
using (Ftp client = new Ftp())
{
client.ConnectSSL("ftp.example.org");
client.Login("username", "password");
foreach (FtpItem item in client.GetList())
{
if (item.IsFolder == true)
Console.WriteLine("[{0}]", item.Name);
else
Console.WriteLine("{0}", item.Name);
}
client.Close();
}
' VB.NET version
Using client As New Ftp()
client.ConnectSSL("ftp.example.org")
client.Login("username", "password")
For Each item As FtpItem In client.GetList()
If item.IsFolder = True Then
Console.WriteLine("[{0}]", item.Name)
Else
Console.WriteLine("{0}", item.Name)
End If
Next
client.Close()
End Using
If your FTP server is using other port than standard 990, you need to use overloaded version of ConnectSSL
// C# version
client.ConnectSSL("ftp.example.org", 999);
' VB.NET version
client.ConnectSSL("ftp.example.org", 999)
The last sample shows how to deal with self-signed certificates:
// C# version
using (Ftp client = new Ftp())
{
// Use this line to validate self-signed certificates:
client.ServerCertificateValidate += ValidateCertificate;
client.ConnectSSL("ftp.example.org");
client.Login("username", "password");
foreach (FtpItem item in client.GetList())
{
if (item.IsFolder == true)
Console.WriteLine("[{0}]", item.Name);
else
Console.WriteLine("{0}", item.Name);
}
client.Close();
}
private static void ValidateCertificate(
object sender,
ServerCertificateValidateEventArgs e)
{
const SslPolicyErrors ignoredErrors =
SslPolicyErrors.RemoteCertificateChainErrors |
SslPolicyErrors.RemoteCertificateNameMismatch;
if ((e.SslPolicyErrors & ~ignoredErrors) == SslPolicyErrors.None)
{
e.IsValid = true;
return;
}
e.IsValid = false;
}
' VB.NET version
Using client As New Ftp()
' Use this line to validate self-signed certificates:
AddHandler client.ServerCertificateValidate, AddressOf ValidateCerificate
client.ConnectSSL("ftp.example.org")
client.Login("username", "password")
For Each item As FtpItem In client.GetList()
If item.IsFolder = True Then
Console.WriteLine("[{0}]", item.Name)
Else
Console.WriteLine("{0}", item.Name)
End If
Next
client.Close()
End Using
Private Sub ValidateCerificate( _
ByVal sender As Object, _
ByVal e As ServerCertificateValidateEventArgs)
Const ignoredErrors As SslPolicyErrors = _
SslPolicyErrors.RemoteCertificateChainErrors Or _
SslPolicyErrors.RemoteCertificateNameMismatch
If (e.SslPolicyErrors And Not ignoredErrors) = SslPolicyErrors.None Then
e.IsValid = True
Return
End If
e.IsValid = False
End Sub
August 23rd, 2016 at 07:41
Where is the ddl’s code ?
August 23rd, 2016 at 08:36
@Rafa,
I’m not sure I understand you. Do you mean ‘Where is the dll’s source code?’
You can download .NET FTP library here.