public string Subject { get; }
A string that contains the distinguished name of the certificate issuer.
The Subject property represents the distinguished name (DN) of the entity to which the certificate was issued. This is typically the name of the server or user for whom the certificate was generated.
The distinguished name is a sequence of relative distinguished names (RDNs) separated by commas. Each RDN consists of a type and a value, such as CN=MyServer.example.com, O=My Organization, or C=US.
For certificates that contain Subject Alternative Names (SANs), the Subject property may not reflect all the host names or IP addresses for which the certificate is valid. In such cases, it is recommended to use the System.Security.Cryptography.X509Certificates.X509Certificate2.GetNameInfo method to retrieve specific name information, including Subject Alternative Names.
The following code example demonstrates how to retrieve and display the Subject property of an X509Certificate2 object.
using System;
using System.Security.Cryptography.X509Certificates;
public class Example
{
public static void Main()
{
// Load a certificate from a file (replace with your certificate path)
string certificatePath = @"C:\Path\To\Your\Certificate.pfx";
string certificatePassword = "your_password";
try
{
X509Certificate2 cert = new X509Certificate2(certificatePath, certificatePassword);
Console.WriteLine($"Certificate Subject: {cert.Subject}");
}
catch (Exception ex)
{
Console.WriteLine($"Error loading certificate: {ex.Message}");
}
}
}