Home Forums .NET libraries Xceed SFTP/FTP for .NET Public key authentication

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Xceed Support
    Member
    Post count: 5658
    #21476 |

    SSHClient supports public key
    authentication. Depending on the requirements of the SSH server, it can
    be used instead of or alongside password authentication.

    To use public key authentication, you need to load a private key file.
    The component supports the PPK format as generated by the PuTTYgen
    utility (an RSA and DSA key generation utility).

    This format was chosen because it is easy to create and
    manipulate keys with the utility. The format is secure. It has good
    support on Windows and the utility can import keys from different
    formats to the PPK format.

    You can get the utility on the PuTTY web site. Direct link to puttygen.exe

    The class

    Xceed.SSH.Client.PuTTYPrivateKeyFile

    implements the PPK file format.

    You load your private key file into the class by using the Read() method
    or the constructor. The class takes the data from a Stream object that
    you supply or from an Xceed.FileSystem.AbstractFile and a passphrase (if
    using) to decrypt the private key.

    The passphrase can be a string or a byte array (generated by encoding the passphrase string into ASCII bytes).

    PuTTYPrivateKeyFile.Read() will throw a SSHIncorrectPasswordException if the passphrase for the private key is incorrect.

    PuTTYPrivateKeyFile.Read() will throw a
    SSHPublicKeyAuthenticationDataException if the private key file is
    malformed, uses unsupported algorithms or if the message authenticity
    code check for the private key fails when an unencrypted private key
    file is used.

    Once you’ve successfully loaded your private key file into a PuTTYPrivateKeyFile object, you supply that object to

    Xceed.SSH.Client.SSHClient.Authenticate()

    the method has a flavor that takes your username string and a
    ISSHPublicKeyAuthenticationData object. PuTTYPrivateKeyFile implements
    this interface.

    Authenticate() will throw a SSHAuthenticationFailedException if the public key is rejected by the server.

    Authenticate() will throw a SSHAuthenticationPartialSuccessException if
    the public key is accepted by the server but more authentications are
    required. If that happens, you can then call Authenticate() again with
    your username and password to attempt the ‘password’ authentication
    method.

    Example (C#)

    AbstractFile privateKeyFile = new DiskFile( @"D:\MyPrivateKey.ppk" );
    string passphrase = "mypassphrase";
    PuTTYPrivateKeyFile privateKey = PuTTYPrivateKeyFile(); privateKey.Read( privateKeyFile, passphrase );
    SSHClient sshClient; sshClient = new SSHClient(); sshClient.Connect( hostName, port );
    sshClient.Authenticate( username, privateKey );

    Applies to Xceed SFTP for .NET. Imported from legacy forums. Posted by Jb [Xceed] (had 1582 views)

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.