Rebex File Transfer Pack

SFTP, FTP, FTP/SSL and File Server for .NET

Download 30-day free trial Buy from $699

Save 33% by buying bundle (Purchased individually: $1047)

More .NET libraries

Back to feature list...

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.

var client = new FileTransferClient();

// connect to a SFTP server
client.Connect(hostname, FileTransferMode.Sftp);
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.

var client = new FileTransferClient();

// connect to a FTP server
client.Connect(hostname, FileTransferMode.Ftp);
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.
var client = new FileTransferClient();

// connect to a FTP/SSL server
client.Connect(hostname, FileTransferMode.FtpSslImplicit);
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:

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

// log in
client.Login(username, password);
' 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:

// 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);
' 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:

// 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);
' 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.

// 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);
' 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
  • X.509 certificate authentication (VanDyke VShell)

For sample code see SFTP API authentication documentation.

Back to feature list...