X509Certificate2.ToString() Method

Represents a collection of X.509 certificates. This class is used to represent an X.509 certificate, which is a digital certificate that uses the X.509 standard. It contains public key information, identity information, and a signature from a certificate authority (CA). The ToString() method provides a string representation of the certificate.

Syntax


public override string ToString();
                

Parameters

This method has no parameters.

Return Value

A string that represents the current certificate. The format of the returned string is not guaranteed to be consistent across different .NET versions or operating systems. Typically, it includes information such as the subject name, issuer name, and serial number.

Remarks

The ToString() method is useful for debugging and logging purposes, allowing developers to quickly inspect the contents of an X509Certificate2 object. It provides a concise summary of the certificate's key attributes.

For more detailed information or to access specific certificate fields, use the properties of the X509Certificate2 class, such as Subject, Issuer, SerialNumber, and Thumbprint.

Example

The following C# code example demonstrates how to obtain and display the string representation of an X.509 certificate.


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

public class Example
{
    public static void Main()
    {
        try
        {
            // Load a certificate from the CurrentUser's My store
            // Replace "YourCertificateName" with the actual name of your certificate
            string certName = "CN=MyTestCert, OU=Example, O=MyCompany, C=US"; // Example subject name
            X509Certificate2 certificate = null;

            using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
            {
                store.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection certCollection = store.Certificates.Find(X509FindType.FindBySubjectName, certName, false);

                if (certCollection.Count > 0)
                {
                    certificate = certCollection[0];
                }
            }

            if (certificate != null)
            {
                // Get the string representation of the certificate
                string certificateString = certificate.ToString();

                // Display the string representation
                Console.WriteLine("Certificate String Representation:");
                Console.WriteLine(certificateString);

                // You can also access individual properties
                Console.WriteLine($"\nSubject: {certificate.Subject}");
                Console.WriteLine($"Issuer: {certificate.Issuer}");
                Console.WriteLine($"Serial Number: {certificate.SerialNumber}");
                Console.WriteLine($"Thumbprint: {certificate.Thumbprint}");
            }
            else
            {
                Console.WriteLine($"Certificate with subject name '{certName}' not found.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}
                

Requirements

Namespace:
System.Net.Security
Assembly:
System.Net.dll