X509Certificate2.Name Property

Return Value

Type: System.String
The distinguished name of the certificate.

Property Value

A string that contains the distinguished name of the certificate.

Remarks

The Name property returns the subject name of the certificate. This name is a string that represents the identity of the owner of the certificate. For example, it might be "CN=Microsoft Corporation, OU=..., O=..., C=US".

The format of the distinguished name is defined by RFC 4514 and can include various attributes like Common Name (CN), Organization (O), Organizational Unit (OU), and Country (C).

If the certificate does not have a subject name, this property will return an empty string.

Example

The following example displays the subject name of an X.509 certificate.

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

public class Example
{
    public static void Main()
    {
        // Load a certificate from the CurrentUser store. Replace "My" with the appropriate store name and "YourCertificateName" with the actual certificate subject name.
        X509Certificate2 cert = null;
        X509Store store = new X509Store("My", StoreLocation.CurrentUser);
        try
        {
            store.Open(OpenFlags.ReadOnly);
            // Find a certificate by its subject name. This is a simplified example; real-world scenarios might need more robust certificate selection.
            foreach (X509Certificate2 c in store.Certificates)
            {
                if (c.Subject.Contains("YourCertificateName")) 
                {
                    cert = new X509Certificate2(c);
                    break;
                }
            }

            if (cert != null)
            {
                Console.WriteLine("Certificate Subject Name: {0}", cert.Name);
            }
            else
            {
                Console.WriteLine("Certificate not found.");
            }
        }
        finally
        {
            if (store != null)
            {
                store.Close();
            }
            if (cert != null)
            {
                cert.Dispose();
            }
        }
    }
}