Represents a configuration section that contains certificate name mapping collections.
Namespace: System.Net.Security
Assembly: System (in System.dll)
The CertificateNameSectionGroup class is used to programmatically access and modify the <certificateName> section within a system.net configuration section. This section is typically found in the machine.config or web.config file.
It allows you to manage certificate name mappings, which are used to validate the names of certificates presented by remote hosts.
using System;
using System.Configuration;
using System.Net.Security;
public class CertificateMappingConfig
{
public static void Main(string[] args)
{
try
{
// Get the configuration object
Configuration config = ConfigurationManager.OpenMachineConfiguration();
// Get the system.net section
SystemNetSection systemNetSection = (SystemNetSection)config.GetSection("system.net");
// Get the certificateName section group
CertificateNameSectionGroup certNameGroup = (CertificateNameSectionGroup)systemNetSection.SectionGroups["certificateName"];
if (certNameGroup != null)
{
// Access the certificate name mappings
CertificateNameMappingCollection mappings = certNameGroup.CertificateNameMappings;
Console.WriteLine("Existing Certificate Mappings:");
foreach (CertificateNameMapping mapping in mappings)
{
Console.WriteLine($"- X509: {mapping.X509FindType}, Thumbprint: {mapping.Thumbprint}, Name: {mapping.Name}");
}
// Example of adding a new mapping (requires write access to config file)
// CertificateNameMapping newMapping = new CertificateNameMapping("myDomain.com", X509FindType.FindByThumbprint, "ABC123DEF456...");
// mappings.Add(newMapping);
// config.Save();
// Console.WriteLine("\nNew mapping added.");
}
else
{
Console.WriteLine("CertificateName section group not found.");
}
}
catch (ConfigurationErrorsException ex)
{
Console.WriteLine($"Configuration error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
Type: CertificateNameMappingCollection
Gets a CertificateNameMappingCollection object that contains the certificate name mappings.