Rebex File Transfer Pack
SFTP/FTP client, SFTP/SSH server libraries for .NET
Download 30-day free trial Buy from $699Save 33% by buying bundle (Purchased individually: $1047)
More .NET libraries
-
Rebex SSH Pack
SSH Shell and SFTP together
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
File Transfer Client: Connect, Authenticate
On this page:
- Connecting to SFTP server
- Connecting to FTP server
- Connecting to FTPS server (FTP over TLS/TLS)
- Authentication - username and password
- Authentication - username, password and account (FTP)
- Authentication - username, password and private key (SFTP)
- Kerberos, NTLM and other SFTP authentication methods
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)
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 FTPS server (FTP over TLS/TLS)
To connect to an FTP server with TLS/SSL encryption, use Connect
method with FileTransferMode.FtpSslImplicit
or FileTransferMode.FtpSslImplicit
argument specified.
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 Rebex FTP 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)
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...