More .NET components

File Transfer Client: Connect, Authenticate

Connecting to SFTP server #

To connect to an SFTP server running on default port 22, use Connect method. SFTP is a file transfer protocol that runs over an SSH protocol session. Most modern SSH servers support SFTP.

CSharp

var client = new FileTransferClient();

// connect to a SFTP server
client.Connect(hostname, FileTransferMode.Sftp);

VisualBasic

Dim client = New FileTransferClient

' connect to a SFTP server
client.Connect(hostname, FileTransferMode.Sftp)

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 server #

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

var client = new FileTransferClient();

// connect to a FTP server
client.Connect(hostname, FileTransferMode.Ftp);

VisualBasic

Dim client = New FileTransferClient

' connect to a FTP server
client.Connect(hostname, FileTransferMode.Ftp)

Connecting to FTP/SSL server #

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

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

CSharp

var client = new FileTransferClient();

// connect to a FTP/SSL server
client.Connect(hostname, FileTransferMode.FtpSslImplicit);

VisualBasic

Dim client = New FileTransferClient

' connect to a FTP/SSL server
client.Connect(hostname, FileTransferMode.FtpSslImplicit)

Server certificate is validated automatically by Rebex File Transfer Client, but you can customize the validation process using the ValidatingCertificate event. For details see certificate validation section in FTP/SSL documentation.

Authentication - username and password #

The most common FTP and SFTP authentication uses username and password:

CSharp

// connect to a server
var client = new FileTransferClient();
client.Connect(hostname, FileTransferMode.Sftp);

// log in
client.Login(username, password);

VisualBasic

' connect to a server
Dim client = New FileTransferClient
client.Connect(hostname, FileTransferMode.Sftp)

' log in
client.Login(username, password)

Some FTP servers allow anonymous authentication. For those, call the Login method with a username of "anonymous" and use an e-mail address as a password.

Authentication - username, password and account (FTP) #

Some FTP servers require an "account name" in addition to the username and password:

CSharp

// connect to a server
var client = new Rebex.Net.FileTransferClient();
client.Connect(hostname, FileTransferMode.Ftp);

// log in using the "account name"
client.Login(username, password, account);

VisualBasic

' connect to a server
Dim client = New Rebex.Net.FileTransferClient()
client.Connect(hostname, FileTransferMode.Ftp)

' log in using the "account name"
client.Login(username, password, account)

Authentication - username, password and private key (SFTP) #

Asymmetric cryptography makes it possible to authenticate using a private key without revealing it to the server (or anyone else) - only the corresponding public key needs to be associated with your account. Use SshPrivateKey class for this kind of authentication:

CSharp

// connect to a server and verify fingerprint
var client = new FileTransferClient();
client.Connect(hostname,FileTransferMode.Sftp);

// load the private key
SshPrivateKey privateKey = new SshPrivateKey("my_key.ppk", "key_password");

// log in
client.Login(username, privateKey);

VisualBasic

' connect to a server and verify fingerprint
Dim client = New FileTransferClient()
client.Connect(hostname, FileTransferMode.Sftp)

' load the private key
Dim privateKey = New SshPrivateKey("my_key.ppk", "key_password")

' log in
client.Login(username, privateKey)

How do you get the private key? Usually, you generate it yourself, either using Rebex KeyGenerator sample, our key-generator API or a third-party utility (most SSH/SFTP vendors provide one). Once generated, the corresponding public key has to be associated with your account (this is server-specific, consult your server administrator if needed).

In case you already have your private key, just load it into the SshPrivateKey object - it supports lot of private key formats.

Kerberos, NTLM and other SFTP authentication methods #

FileTransferClient supports also other, less common, SFTP authentication options.

Following code shows how to authenticate using a Kerberos credentials.

CSharp

// connect to a server and verify fingerprint
var client = new FileTransferClient();
client.Connect(hostname, FileTransferMode.Sftp);

// initialize GSSAPI for Kerberos authentication
var credentials = new SshGssApiCredentials(username, password, domain);
credentials.SetMechanisms(SshGssApiMechanisms.KerberosV5);

// log in using Kerberos
client.Login(credentials);

VisualBasic

' connect to a server and verify fingerprint
Dim client = New FileTransferClient()
client.Connect(hostname,FileTransferMode.Sftp)

' initialize GSSAPI for Kerberos authentication
Dim credentials = New SshGssApiCredentials(username, password, domain)
credentials.SetMechanisms(SshGssApiMechanisms.KerberosV5)

' log in using Kerberos
client.Login(credentials)

Other SFTP/SSH authentication methods

  • GSSAPI Kerberos with username/password/domain
  • GSSAPI Kerberos with single sign-on
  • GSSAPI NTLM with username/password/domain
  • GSSAPI NTLM with single sign-on
  • X509 certificate authentication (VanDyke VShell)

For sample code see SFTP API authentication documentation.