PowerTCP FTP for .NET
FtpSecurity Class
Members  Example 



Specializes the ClientSecurity class with properties used by FTP.
Syntax
<SerializableAttribute()>
Public Class FtpSecurity 
   Inherits ClientSecurity
Dim instance As FtpSecurity
[Serializable()]
public class FtpSecurity : ClientSecurity 
[Serializable()]
public ref class FtpSecurity : public ClientSecurity 
Example
The following example demonstrates connecting to an Ftp server, with options for encryption (FTPS: Explicit/Implicit), and authenticating the user.
using System.Security.Cryptography.X509Certificates;
using System.Security.Authentication;
using System.Net.Security;

/// <summary>
/// Connects to an Ftp server, optionally using encryption (Explicit/Implicit), and authenticates the user.
/// </summary>
/// <param name="myFtp">The Ftp instance to connect and authenticate</param>
/// <param name="hostNameOrAddress">The server's hostname or IP address</param>
/// <param name="port">Port that the server is listening on. Usually 21 for Explicit or non-secure, 990 for Implicit.</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="encryptControl">Controls whether SSL/TLS is used, and the implementation. None/Explicit/Implicit.</param>
/// <param name="encryptData">Controls whether the data channel is encrypted.</param>
private void ConnectFtp(Ftp myFtp, string hostNameOrAddress, int port, string username, string password, EncryptControl encryptControl, bool encryptData)
{
    //Set the server address
    myFtp.Session.RemoteEndPoint = new IPEndPoint(hostNameOrAddress, port);

    if (encryptControl != EncryptControl.None)
    {
        //Set the control channel's security protocol - Implicit/Explicit
        myFtp.Session.Security.EncryptControl = encryptControl;

        //Set whether the data channel should be encrypted. This may or may not be required by your FTP server.
        myFtp.Session.Security.EncryptData = encryptData;

        //Optionally set the protocols available for SSL/TLS negotiation (defaults to SslProtocols.Default)
        //TLS 1.1/1.2 requires .NET 4.5+. See the SslProtocols MSDN documentation for more information.
        myFtp.Session.Security.Protocols = SslProtocols.Tls | SslProtocols.Ssl3;

        //Specify the server certificate validation callback
        myFtp.Session.Security.ValidationCallback = remoteCertificateValidation;
    }

    //Connect to the server.
    myFtp.Connect();

    //Authenticate the user.
    myFtp.Authenticate(username, password);
}

private bool remoteCertificateValidation(Object sender, X509Certificate remoteCertificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    //For this simple snippet, accept all server certificates. Please see the 'Security' top-level help topics for more information, or 
    //the System.Net.Security.RemoteCertificateValidationCallback MSDN documentation.
    return true;
}
Imports System.Security.Cryptography.X509Certificates
Imports System.Security.Authentication
Imports System.Net.Security

''' <summary>
''' Connects to an Ftp server, optionally using encryption (Explicit/Implicit), and authenticates the user.
''' </summary>
''' <param name="myFtp">The Ftp instance to connect and authenticate</param>
''' <param name="hostNameOrAddress">The server's hostname or IP address</param>
''' <param name="port">Port that the server is listening on. Usually 21 for Explicit or non-secure, 990 for Implicit.</param>
''' <param name="username">Username</param>
''' <param name="password">Password</param>
''' <param name="encryptControl">Controls whether SSL/TLS is used, and the implementation. None/Explicit/Implicit.</param>
''' <param name="encryptData">Controls whether the data channel is encrypted.</param>
Private Sub ConnectFtp(ByVal myFtp As Ftp, ByVal hostNameOrAddress As String, ByVal port As Integer, ByVal username As String, ByVal password As String, ByVal encryptControl As EncryptControl, ByVal encryptData As Boolean)
    'Set the server address
    myFtp.Session.RemoteEndPoint = New IPEndPoint(hostNameOrAddress, port)

    If encryptControl <> Dart.Ftp.EncryptControl.None Then
        'Set the control channel's security protocol - Implicit/Explicit
        myFtp.Session.Security.EncryptControl = encryptControl

        'Set whether the data channel should be encrypted. This may or may not be required by your FTP server.
        myFtp.Session.Security.EncryptData = encryptData

        'Optionally set the protocols available for SSL/TLS negotiation (defaults to SslProtocols.Default)
        'TLS 1.1/1.2 requires .NET 4.5+. See the SslProtocols MSDN documentation for more information.
        myFtp.Session.Security.Protocols = SslProtocols.Tls Or SslProtocols.Ssl3

        'Specify the server certificate validation callback
        myFtp.Session.Security.ValidationCallback = AddressOf remoteCertificateValidation
    End If

    'Connect to the server.
    myFtp.Connect()

    'Authenticate the user.
    myFtp.Authenticate(username, password)
End Sub

Private Function remoteCertificateValidation(ByVal sender As Object, ByVal remoteCertificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
    'For this simple snippet, accept all server certificates. Please see the 'Security' top-level help topics for more information, or 
    'the System.Net.Security.RemoteCertificateValidationCallback MSDN documentation.
    Return True
End Function
Inheritance Hierarchy

System.Object
   Dart.Ftp.Security
      Dart.Ftp.ClientSecurity
         Dart.Ftp.FtpSecurity

See Also

Reference

FtpSecurity Members
Dart.Ftp Namespace


PowerTCP FTP for .NET Documentation Version 4.7
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic