More .NET components

Connecting

Connecting to FTP servers #

To connect to an FTP server running on default port 21, use Connect method. In this case, a plain (unencrypted) FTP connection will be established.

CSharp

// create FTP client instance
var ftp = new Rebex.Net.Ftp();

// connect to a server
ftp.Connect(hostname);

VisualBasic

' create FTP client instance
Dim ftp = New Rebex.Net.Ftp

' connect to a server
ftp.Connect(hostname)

To connect to a server running on a non-standard port, just pass the port number to the Connect method as an additional argument.

Connecting to FTP servers using Explicit or Implicit SSL #

To connect to an FTP server with TLS/SSL encryption, use Connect method with SslMode argument specified.

Tip: For more information about SslMode, see differences between Explicit and Implicit modes.

CSharp

// create FTP client instance
var ftp = new Rebex.Net.Ftp();

// connect to a server using explicit TLS/SSL
// (by default, port 21 is used)
ftp.Connect(hostname, SslMode.Explicit);

// connect to a server using implicit TLS/SSL
// (by default, port 990 is used)
//ftp.Connect(hostname, SslMode.Implicit);

VisualBasic

' create FTP client instance
Dim ftp = New Rebex.Net.Ftp

' connect to a server using explicit TLS/SSL
' (by default, port 21 is used)
ftp.Connect(hostname, SslMode.Explicit)

' connect to a server using implicit TLS/SSL
' (by default, port 990is used)
ftp.Connect(hostname, SslMode.Implicit)

Server certificate is validated automatically by Rebex FTP/SSL, but you can customize the validation process as well.

Tip: For advanced security options, see TLS/SSL core section.

Switching to encrypted communication #

It's also possible to connect using plain (unencrypted) FTP first and upgrade to TLS/SSL later using Secure method.

CSharp

// create FTP client instance
var ftp = new Rebex.Net.Ftp();

// connect to a server on default (not-secured) port 21
ftp.Connect(hostname);

// secure connection
ftp.Secure();

// authenticate securely
ftp.Login(username, password);

VisualBasic

' create FTP client instance
Dim ftp = New Rebex.Net.Ftp

' connect to a server on default (not-secured) port 21
ftp.Connect(hostname)

' secure connection
ftp.Secure()

' authenticate securely
ftp.Login(username, password)

Calling (plain-mode) Connect followed by Secure is equivalent to calling Connect(serverName, SslMode.Explicit).

Reverting to unencrypted communication #

After authenticating, it's possible to revert an encrypted connection back to unencrypted using the ClearCommandChannel method. This is useful when there is an FTP-aware firewall along the way that only functions properly when it can analyze the FTP communication.

This only reverts the control connection. Data transfers and file listings are still encrypted (unless SecureTransfers property has been set to false).

CSharp

// create FTP client instance
var ftp = new Rebex.Net.Ftp();

// connect to a server using Explicit SSL
ftp.Connect(hostname, SslMode.Explicit);

// authenticate securely
ftp.Login(username, password);

// keep data transfers secured
// this is the default mode (it's not necessary to set it)
ftp.SecureTransfers = true;

// revert control connection to unencrypted communication
// anyone e.g. firewall can see commands sent
ftp.ClearCommandChannel();

VisualBasic

' create FTP client instance
Dim ftp = New Rebex.Net.Ftp

' connect to a server using Explicit SSL
ftp.Connect(hostname, SslMode.Explicit)

' authenticate securely
ftp.Login(username, password)

' keep data transfers secured
' this is the default mode (it's not necessary to set it)
ftp.SecureTransfers = True

' revert control connection to unencrypted communication
' anyone e.g. firewall can see commands sent
ftp.ClearCommandChannel()

Enabling/disabling data transfer encryption #

To enable or disable data transfer encryption, use SecureTransfers property.

CSharp

// create FTP client instance, connect with SSL, log in
// ...

// turn off data transfer encryption
ftp.SecureTransfers = false;

// transfer files
// ...

VisualBasic

' create FTP client instance, connect with SSL, log in
' ...

' turn off data transfer encryption
ftp.SecureTransfers = False

' transfer files
' ...

In non-secure mode, SecureTransfers property is ignored.

Setting connection options #

There are various connection options such as proxies, transfer modes, character sets, FTP extensions, and more.

CSharp

// create FTP client instance, specify proxy, connect, log in
// ...

// set active mode
ftp.Passive = true;

// set UTF-8 encoding for commands and responses
ftp.Encoding = Encoding.UTF8;

// use large transfer buffers
ftp.Settings.UseLargeBuffers = true;

// disable MLST extension
ftp.EnabledExtensions &= ~FtpExtensions.MachineProcessingList;

// transfer files
// ...

VisualBasic

' create FTP client instance, connect, log in
' ...

' set active mode
ftp.Passive = False

' set UTF-8 encoding for commands and responses
ftp.Encoding = Encoding.UTF8

' use large transfer buffers
ftp.Settings.UseLargeBuffers = True

' disable MLST extension
ftp.EnabledExtensions = ftp.EnabledExtensions And Not FtpExtensions.MachineProcessingList

' transfer files
' ...

Getting info about existing connection #

Once connected (or authenticated), you can get useful information about the current connection such as server name, port, OS or user name. Don't be afraid - user's password is not stored with the connection.

CSharp

// create FTP client instance
var ftp = new Rebex.Net.Ftp();

// connect to a server
ftp.Connect(hostname);

// display info
Console.WriteLine("Connected to {0}:{1} ({2}).",
    ftp.ServerName, ftp.ServerPort, ftp.GetSystemName());

// authenticate
ftp.Login(username, password);

// display info to the user
Console.WriteLine("Logged on as {0}.", ftp.UserName);

VisualBasic

' create FTP client instance
Dim ftp = New Rebex.Net.Ftp()

' connect to a server
ftp.Connect(hostname)

' display info
Console.WriteLine("Connected to {0}:{1} ({2}).",
    ftp.ServerName, ftp.ServerPort, ftp.GetSystemName())

' authenticate
ftp.Login(username, password)

' display info to the user
Console.WriteLine("Logged on as {0}.", ftp.UserName)

Getting info about SSL connection #

To get information about SSL connection, use TlsSocket property.

CSharp

// create FTP client instance
var ftp = new Rebex.Net.Ftp();

// connect to a server
ftp.Connect(hostname, SslMode.Explicit);

// Cipher property contains a lot of information about the current cipher
Console.WriteLine("TLS cipher info: {0}", ftp.TlsSocket.Cipher);

// ServerCertificate property provides access to server certificate
// the first certificate in the chain is the server's certificate
CertificateChain chain = ftp.TlsSocket.ServerCertificate;
Console.WriteLine("Server's certificate info: Subject: {0}, Issuer: {1}",
    chain[0].GetSubject(), chain[0].GetIssuer());

VisualBasic

' create FTP client instance
Dim ftp = New Rebex.Net.Ftp()

' connect to a server
ftp.Connect(hostname, SslMode.Explicit)

' Cipher property contains a lot of information about the current cipher
Console.WriteLine("TLS cipher info: {0}", ftp.TlsSocket.Cipher)

' ServerCertificate property provides access to server certificate
' the first certificate in the chain is the server's certificate
Dim chain As CertificateChain = ftp.TlsSocket.ServerCertificate
Console.WriteLine("Server's certificate info: Subject: {0}, Issuer: {1}",
    chain(0).GetSubject(), chain(0).GetIssuer())

Checking connection state #

To check whether a connection is still alive, use GetConnectionState method. This will send a simple "read-only" command to the server to ensure the connection is still up and running.

CSharp

// check connection state
if (!ftp.GetConnectionState().Connected)
{
    // notify the user and return
    return;
}

VisualBasic

' check connection state
If Not ftp.GetConnectionState().Connected Then
    ' notify the user and return
    Return
End If