X509Certificate2.Subject Property

Namespace:
System.Net.Security
Assembly:
System.Security.Cryptography.X509Certificates.dll
Inheritance Hierarchy:
System.Object
    System.Security.Cryptography.X509Certificates.X509Certificate
        System.Security.Cryptography.X509Certificates.X509Certificate2
            System.Net.Security.X509Certificate2

Syntax


public string Subject { get; }
        

Property Value

A string that contains the distinguished name of the certificate issuer.

Remarks

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.

Note

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.

Examples

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}");
        }
    }
}
        

Requirements

Minimum supported client
Windows XP SP3
Minimum supported server
Windows Server 2003 SP2
Framework
.NET Framework 4.0, .NET Core 2.0, .NET Standard 1.3
Namespace
System.Net.Security
Assembly
System.Security.Cryptography.X509Certificates.dll

See Also