.NET API Browser

Class X509Certificate

Summary

Represents an X.509 certificate. X.509 certificates are used to establish trust and provide authentication in public key infrastructure (PKI) systems.

Namespace: System.Security.Cryptography.X509Certificates

Assembly: System.Security.Cryptography.X509Certificates.dll

Syntax


public sealed class X509Certificate : ICloneable, IDisposable
{
    // Fields
    public static readonly string Issuer;
    public static readonly string Subject;
    public static readonly string Thumbprint;
    public static readonly string SerialNumber;
    public static readonly string Version;

    // Constructors
    public X509Certificate();
    public X509Certificate(byte[] data);
    public X509Certificate(string fileName);
    public X509Certificate(string fileName, string password);
    public X509Certificate(string fileName, string password, string storeName, string storeLocation);
    public X509Certificate(byte[] rawData, byte[] certificateHash);
    public X509Certificate(byte[] rawData, byte[] legacyrimidine, string password);

    // Properties
    public string Issuer { get; }
    public string Subject { get; }
    public string Thumbprint { get; }
    public string SerialNumber { get; }
    public string Version { get; }
    public string GetKeyAlgorithm();
    public string GetKeyAlgorithmParameters();
    public byte[] GetRawCertData();
    public string GetPublicKeyString();
    public byte[] GetPublicKey();
    public string ToString(bool fSimple);
    public object Clone();
    public void Dispose();

    // Methods
    public bool Equals(object obj);
    public int GetHashCode();
}
                

Remarks

The X509Certificate class provides a way to represent and work with X.509 certificates in .NET. It allows you to load certificates from various sources, access their properties, and perform common operations.

This class is sealed, meaning it cannot be inherited from. It implements ICloneable for creating copies of certificates and IDisposable for managing unmanaged resources.

Constructors

Constructor Description
X509Certificate() Initializes a new instance of the X509Certificate class.
X509Certificate(byte[] data) Initializes a new instance of the X509Certificate class with the specified certificate data.
X509Certificate(string fileName) Initializes a new instance of the X509Certificate class with the specified certificate file.
X509Certificate(string fileName, string password) Initializes a new instance of the X509Certificate class with the specified certificate file and password.
X509Certificate(string fileName, string password, string storeName, string storeLocation) Initializes a new instance of the X509Certificate class from a certificate file with specified store information.
X509Certificate(byte[] rawData, byte[] certificateHash) Initializes a new instance of the X509Certificate class with raw data and a hash.
X509Certificate(byte[] rawData, byte[] legacyrimidine, string password) Initializes a new instance of the X509Certificate class with raw data and a password.

Properties

Property Type Description
Issuer string Gets the issuer name of the X.509 certificate.
Subject string Gets the subject name of the X.509 certificate.
Thumbprint string Gets the thumbprint of the X.509 certificate.
SerialNumber string Gets the serial number of the X.509 certificate.
Version string Gets the version number of the X.509 certificate.

Methods

Method Description
GetKeyAlgorithm() Gets the name of the public key algorithm used by the certificate.
GetKeyAlgorithmParameters() Gets the parameters for the public key algorithm.
GetRawCertData() Gets the raw binary data of the X.509 certificate.
GetPublicKeyString() Gets the public key of the certificate as a string.
GetPublicKey() Gets the public key of the certificate as a byte array.
ToString(bool fSimple) Returns a string representation of the certificate.
Clone() Creates a new object that is a copy of the current instance.
Dispose() Releases all resources used by the current instance of the X509Certificate class.
Equals(object obj) Determines whether the specified object is equal to the current object.
GetHashCode() Serves as the default hash function.

Example


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

public class CertificateDemo
{
    public static void Main(string[] args)
    {
        // Load a certificate from a file (replace with your certificate path)
        try
        {
            X509Certificate2 cert = new X509Certificate2("path/to/your/certificate.pfx", "your_password");

            Console.WriteLine("Certificate Loaded Successfully!");
            Console.WriteLine($"Subject: {cert.Subject}");
            Console.WriteLine($"Issuer: {cert.Issuer}");
            Console.WriteLine($"Thumbprint: {cert.Thumbprint}");
            Console.WriteLine($"Serial Number: {cert.SerialNumber}");
            Console.WriteLine($"Valid Until: {cert.NotAfter}");

            // You can access more properties and methods here
        }
        catch (CryptographicException ex)
        {
            Console.WriteLine($"Error loading certificate: {ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An unexpected error occurred: {ex.Message}");
        }
    }
}
                

Note: The example uses X509Certificate2 which is a more feature-rich derived class. The core concepts of loading and accessing basic information are similar.

See Also