Authentication
After establishing a connection to an SSH server, you must authenticate before you can execute commands or transfer files. NullOpsDevs.LibSsh supports multiple authentication methods to suit different security requirements and deployment scenarios.
Overview
Authentication is performed by calling the Authenticate() method on your SshSession instance with an appropriate SshCredential object. The session must be in the Connected state before authenticating.
Password Authentication
Password authentication is the simplest method but is generally less secure than key-based authentication. It's suitable for development environments or when key-based authentication isn't available.
Public Key Authentication (File-based)
Public key authentication is the recommended method for most use cases. It uses asymmetric cryptography where you authenticate with a private key that corresponds to a public key registered on the server.
Basic Usage
With Encrypted Private Key
Public Key Authentication (Memory-based)
When you need to load keys from sources other than the filesystem (e.g., databases, configuration systems, or encrypted stores), you can use memory-based authentication.
SSH Agent Authentication
SSH agent authentication delegates key management to an SSH agent (ssh-agent on Linux/macOS, Pageant on Windows). This method is convenient when you have multiple keys or want to avoid storing private keys in your application.
The SSH agent will:
Try each available identity in the agent
Return success when a valid key is found
Return failure if no keys authenticate successfully
Host-based Authentication
Host-based authentication allows a trusted client host to authenticate users without requiring individual credentials. This method is rarely used in modern deployments and is typically restricted to tightly controlled environments.
Error Handling
Authentication failures can occur for various reasons. Always check the return value and handle failures appropriately:
Common authentication failures:
Invalid credentials: Wrong password or key not authorized on server
Key format issues: Unsupported key type or corrupted key file
Permission denied: Server configuration doesn't allow the authentication method
Agent not available: SSH agent not running when using agent authentication
See Also
Host Key Retrieval and Verification - Verify server identity before authenticating
Session Lifecycle - Understanding session states during authentication
Command Execution - Execute commands after authenticating
File Transfer with SCP - Transfer files after authenticating
Error Handling - Handle authentication errors
Quickstart - Complete connection and authentication examples