System.Net.Security.SslStream.AuthenticateAsClient

This topic describes the AuthenticateAsClient method of the SslStream class, which is used to establish a secure client connection.

public virtual bool AuthenticateAsClient(string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Cryptography.Oid[] acceptableIssuers, System.Security.Cryptography.Oid[] acceptableKeyExchangeKeyUsages, System.Security.Cryptography.Oid[] acceptableSignatureAlgorithms)
public virtual bool AuthenticateAsClient(string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Cryptography.Pkcs.ContentInfo[] acceptableIssuers, System.Security.Cryptography.Pkcs.ContentInfo[] acceptableKeyExchangeKeyUsages, System.Security.Cryptography.Pkcs.ContentInfo[] acceptableSignatureAlgorithms)
public virtual bool AuthenticateAsClient(string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Cryptography.Oid acceptableIssuers, System.Security.Cryptography.Oid acceptableKeyExchangeKeyUsages, System.Security.Cryptography.Oid acceptableSignatureAlgorithms)
public virtual bool AuthenticateAsClient(string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Cryptography.Pkcs.ContentInfo acceptableIssuers, System.Security.Cryptography.Pkcs.ContentInfo acceptableKeyExchangeKeyUsages, System.Security.Cryptography.Pkcs.ContentInfo acceptableSignatureAlgorithms)
public virtual bool AuthenticateAsClient(string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Cryptography.Oid acceptableIssuers, System.Security.Cryptography.Oid acceptableKeyExchangeKeyUsages)
public virtual bool AuthenticateAsClient(string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Cryptography.Pkcs.ContentInfo acceptableIssuers, System.Security.Cryptography.Pkcs.ContentInfo acceptableKeyExchangeKeyUsages)
public virtual bool AuthenticateAsClient(string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Cryptography.Oid acceptableIssuers)
public virtual bool AuthenticateAsClient(string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Cryptography.Pkcs.ContentInfo acceptableIssuers)
public virtual bool AuthenticateAsClient(string targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates)
public virtual bool AuthenticateAsClient(string targetHost)

Syntax

The SslStream.AuthenticateAsClient method is used by the client side of a Secure Sockets Layer (SSL) connection to establish a secure channel with a server. This method initiates the SSL handshake process.

Parameters

Parameter Type Description
targetHost System.String The host name of the server to authenticate. This parameter is used to validate the server's certificate.
clientCertificates System.Security.Cryptography.X509Certificates.X509CertificateCollection A collection of client certificates to present to the server. Can be null.
acceptableIssuers System.Security.Cryptography.Oid[] or System.Security.Cryptography.Pkcs.ContentInfo[] An array of Object Identifiers (OIDs) or ContentInfo structures specifying the acceptable certificate issuers. Can be null.
acceptableKeyExchangeKeyUsages System.Security.Cryptography.Oid[] or System.Security.Cryptography.Pkcs.ContentInfo[] An array of OIDs or ContentInfo structures specifying the acceptable key exchange key usages. Can be null.
acceptableSignatureAlgorithms System.Security.Cryptography.Oid[] or System.Security.Cryptography.Pkcs.ContentInfo[] An array of OIDs or ContentInfo structures specifying the acceptable signature algorithms. Can be null.

Return Value

true if the authentication was successful; otherwise, false.

Exceptions

Remarks

The AuthenticateAsClient method must be called before any data is sent or received over the SslStream. The targetHost parameter is crucial for verifying the server's identity. If the server's certificate does not match the specified targetHost, the authentication will fail.

When providing client certificates, ensure they are valid and trusted by the server.

Important: For modern security practices, it is highly recommended to use TLS 1.2 or higher. Ensure your SslProtocols enumeration is configured appropriately when creating the SslStream.

Example


using System;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

public class SslClientExample
{
    public static async Task ConnectAndCommunicateAsync(string host, int port)
    {
        using (TcpClient client = new TcpClient())
        {
            await client.ConnectAsync(host, port);
            using (SslStream sslStream = new SslStream(client.GetStream(), false))
            {
                try
                {
                    // Authenticate the client
                    // Use the server's hostname for targetHost validation
                    // No client certificate is provided in this example
                    await sslStream.AuthenticateAsClientAsync(host);

                    Console.WriteLine("SSL handshake completed successfully.");

                    // Now you can send and receive data securely
                    byte[] message = System.Text.Encoding.UTF8.GetBytes("Hello, server!");
                    await sslStream.WriteAsync(message, 0, message.Length);
                    await sslStream.FlushAsync();

                    byte[] buffer = new byte[2048];
                    int bytesRead = await sslStream.ReadAsync(buffer, 0, buffer.Length);
                    string response = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead);
                    Console.WriteLine($"Server response: {response}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Authentication failed: {ex.Message}");
                }
            }
        }
    }

    public static async Task Main(string[] args)
    {
        // Replace with your target server details
        string serverHost = "example.com";
        int serverPort = 443;

        await ConnectAndCommunicateAsync(serverHost, serverPort);
    }
}
            

See Also