CertificateMapper Class

Namespace

System.Net.Security

Assembly

System (in System.dll)

Syntax

public abstract class CertificateMapper

Remarks

The CertificateMapper class is an abstract base class that enables you to provide custom logic for mapping server certificates to client certificates.

This class is particularly useful in scenarios where you need to perform certificate-based authentication between a client and a server, and the default certificate selection mechanisms are not sufficient.

Developers can inherit from CertificateMapper and override its methods to implement their own certificate mapping strategies.

Methods

MapServerCertificate()

Abstract method that must be implemented by derived classes.

This method is called by the client to map a server certificate to a client certificate. The server certificate is provided as input, and the method should return the appropriate client certificate, or null if no suitable client certificate is found.

public abstract X509Certificate2 MapServerCertificate(X509Certificate2 serverCertificate)

Parameters

Name Type Description
serverCertificate System.Security.Cryptography.X509Certificates.X509Certificate2 The certificate presented by the server.

Return Value

An X509Certificate2 object representing the client certificate to be used for authentication, or null if no mapping is found.

Derived Classes

The CertificateMapper class is intended to be inherited from. You can create your own implementations of this class to customize certificate mapping behavior.

Example:

CustomCertificateMapper.cs

using System;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public class CustomCertificateMapper : CertificateMapper
{
    public override X509Certificate2 MapServerCertificate(X509Certificate2 serverCertificate)
    {
        // Logic to find and return a suitable client certificate based on the serverCertificate
        // For example, you might look for a client certificate with a specific issuer or subject name.

        Console.WriteLine($"Mapping server certificate: {serverCertificate.Subject}");

        // Placeholder: In a real scenario, you would search your certificate store
        // or a file for the appropriate client certificate.
        X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly);

        X509Certificate2 clientCertificate = null;
        foreach (X509Certificate2 cert in store.Certificates)
        {
            // Example mapping criteria: match based on issuer or part of subject name
            if (cert.Issuer.Contains("MyCompany") && serverCertificate.Subject.Contains("SecureServer"))
            {
                clientCertificate = cert;
                break;
            }
        }
        store.Close();

        if (clientCertificate != null)
        {
            Console.WriteLine($"Found client certificate: {clientCertificate.Subject}");
        }
        else
        {
            Console.WriteLine("No suitable client certificate found.");
        }

        return clientCertificate;
    }
}

To use this custom mapper, you would typically assign an instance of it to a relevant property in your network client implementation (e.g., in SslClientAuthenticationOptions or a custom HttpClientHandler).

Requirements

Prerequisites:

  • .NET Framework 4.5 or later
  • System.dll assembly