System.Net.Security.NetIpClientAuthentication Class

System.Net.Security

Summary

This class provides client authentication services for secure network connections using the Network IP protocol. It enables secure communication by ensuring the identity of the client application.

Syntax

public static class NetIpClientAuthentication

Remarks

The NetIpClientAuthentication class is designed to facilitate secure client authentication within the .NET framework, particularly for applications that utilize the Network IP protocol. It abstracts the complexities of cryptographic operations and certificate management, providing a straightforward interface for developers to implement robust security measures.

Key features include:

Developers can leverage this class to build secure client applications that require authenticated access to network resources, ensuring data integrity and confidentiality.

Methods

AuthenticateAsClient

public static void AuthenticateAsClient(
    string targetHost,
    System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates,
    System.Net.Security.SslProtocols enabledSslProtocols,
    bool checkCertificateRevocation
)

Authenticates the client using the specified parameters to establish a secure connection.

Parameters:
  • targetHost: The name of the server to authenticate.
  • clientCertificates: A collection of client certificates to use for authentication.
  • enabledSslProtocols: The SSL/TLS protocols to use for the connection.
  • checkCertificateRevocation: A boolean value indicating whether to check for certificate revocation.
Returns:

void

AuthenticateAsClient (Overload)

public static System.Net.Security.SslStream AuthenticateAsClient(
    string targetHost,
    System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates,
    System.Net.Security.SslProtocols enabledSslProtocols,
    bool checkCertificateRevocation,
    System.Net.Security.RemoteCertificateValidationCallback userCertificateValidationCallback
)

Authenticates the client and returns an SslStream for secure communication.

Parameters:
  • targetHost: The name of the server to authenticate.
  • clientCertificates: A collection of client certificates to use for authentication.
  • enabledSslProtocols: The SSL/TLS protocols to use for the connection.
  • checkCertificateRevocation: A boolean value indicating whether to check for certificate revocation.
  • userCertificateValidationCallback: A callback delegate to validate the server's certificate.
Returns:

An SslStream object that provides secure communication.

Examples

Here's a simple example demonstrating how to use NetIpClientAuthentication to connect to a secure server:

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

public class ClientExample
{
    public static async Task ConnectToServer(string host, int port)
    {
        try
        {
            using (var client = new TcpClient(host, port))
            {
                using (var stream = client.GetStream())
                {
                    // In a real scenario, you would select appropriate SSL protocols
                    // and potentially load client certificates.
                    var sslStream = new SslStream(stream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);

                    await sslStream.AuthenticateAsClientAsync(host, null, SslProtocols.Tls12, false);

                    Console.WriteLine("Client authenticated successfully.");

                    // Now you can send and receive data securely over sslStream
                    // ...
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }

    public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        // In a production environment, you would implement robust certificate validation logic.
        // For this example, we'll accept any valid certificate for demonstration purposes.
        if (sslPolicyErrors == SslPolicyErrors.None)
        {
            return true;
        }

        Console.WriteLine($"Certificate error: {sslPolicyErrors}");
        return false; // Reject the certificate
    }

    // Example of how to call the connect method:
    // public static async Task Main(string[] args)
    // {
    //     await ConnectToServer("secure.example.com", 443);
    // }
}

Requirements

Namespace: System.Net.Security

Assembly: System.Net.Security.dll

Applies to: .NET Framework 4.5, .NET Core 1.0, .NET Standard 1.3, .NET 5+, .NET 6+

Last updated: October 15, 2023