SSH Code Example: Public key authentication

An example of how to perform public key authentication.

Date Entered: 07/25/2008    Last Updated: 07/25/2008

In addition to password authentication, the SSH components can also be configured to perform public key authentication. To specify the type of authentication to use set the SSHAuthMode property to one of the supported values. Supported AuthMethods are:

  • amNone - The user will be logged in anonymously.
  • amMultiFactor - The component will perform a multi-step authentication.
  • amPassword - Standard user/password authentication.
  • amPublicKey - The private key will be used in conjunction with the user to authenticate.

When performing public key authentication, you will specify the private key in the SSHCert property of the component. The server should already be configured with the corresponding public key.

An example of specifying a private key in PEM Format is below:

C#
sftp.SSHUser = "test";
sftp.SSHCert = new Certificate(CertStoreTypes.cstPEMKeyFile, "..\\id_rsa.priv", "password", "");
sftp.SSHAuthMode = SftpSSHAuthModes.amPublicKey;
The components support both RSA and DSA private keys.

Note: If your private key is a .ppk file please reference KB Entry 06120702


We appreciate your feedback.  If you have any questions, comments, or suggestions about this entry please contact our support team at kb@nsoftware.com.