X509Certificate2 Class
Represents an X.509 certificate.
The X509Certificate2 class provides a managed wrapper for X.509 certificates. It allows you to access various properties of the certificate, such as its subject, issuer, expiration date, and public key. This class is crucial for establishing secure communication channels using protocols like SSL/TLS.
Constructors
X509Certificate2()
Initializes a new instance of the X509Certificate2 class.
X509Certificate2(byte[] rawData)
Initializes a new instance of the X509Certificate2 class using the specified array of bytes.
Parameters
- rawData: A
byte[]array containing the encoded certificate.
X509Certificate2(string fileName)
Initializes a new instance of the X509Certificate2 class using the certificate file at the specified path.
Parameters
- fileName: The path to the certificate file.
X509Certificate2(string fileName, string password)
Initializes a new instance of the X509Certificate2 class using the certificate file at the specified path and password.
Parameters
- fileName: The path to the certificate file.
- password: The password for the certificate file.
Properties
Archived
Gets or sets a value indicating whether the certificate is archived.
Extensions
Gets an X509ExtensionCollection collection of extensions contained in the certificate.
FriendlyName
Gets or sets a friendly name for the certificate.
HasPrivateKey
Gets a value indicating whether the certificate has an associated private key.
Issuer
Gets the issuer name from the certificate.
IssuerName
Gets the issuer name from the certificate as an X500DistinguishedName object.
KeyAlgorithm
Gets the name of the public key algorithm used by the certificate.
KeyLength
Gets the length of the public key, in bits, used by the certificate.
NotAfter
Gets the date and time after which the certificate is no longer valid.
NotBefore
Gets the date and time at which the certificate becomes valid.
Os Issuer
Gets the issuer name from the certificate, suitable for display.
Os Subject
Gets the subject name from the certificate, suitable for display.
PrivateKey
Gets the private key associated with the certificate.
PublicKey
Gets the public key associated with the certificate.
RawData
Gets the raw data of the certificate.
SerialNumber
Gets the serial number of the certificate.
SignatureAlgorithm
Gets the name of the signature algorithm used to sign the certificate.
Subject
Gets the subject name from the certificate.
SubjectName
Gets the subject name from the certificate as an X500DistinguishedName object.
Thumbprint
Gets the thumbprint of the certificate.
ThumbprintFromHashAlgorithm
Gets the thumbprint of the certificate using the specified hash algorithm.
Version
Gets the version number of the certificate.
Methods
Export(X509ContentType contentType)
Exports the certificate to a byte array in the specified format.
Parameters
- contentType: The format for the exported certificate.
Returns
A byte array containing the exported certificate.
Export(X509ContentType contentType, string password)
Exports the certificate to a byte array in the specified format with a password.
Parameters
- contentType: The format for the exported certificate.
- password: The password for protecting the private key.
Returns
A byte array containing the exported certificate.
GetCertHash()
Gets the hash of the certificate.
Returns
A byte array representing the certificate hash.
GetCertHashString()
Gets the hash of the certificate as a string.
Returns
A string representing the certificate hash.
GetExpirationDateString()
Gets the expiration date of the certificate as a string.
Returns
A string representing the expiration date.
GetFormat()
Gets the format of the certificate.
Returns
A string representing the certificate format.
GetInvalidDateString()
Gets a string representing the date the certificate becomes invalid.
Returns
A string representing the invalid date.
GetKeyAlgorithm()
Gets the name of the key algorithm used by the certificate.
Returns
A string representing the key algorithm.
GetKeyAlgorithmParameters()
Gets the parameters for the key algorithm used by the certificate.
Returns
A byte array containing the key algorithm parameters.
GetPublicKeyString()
Gets the public key of the certificate as a string.
Returns
A string representing the public key.
GetRawCertData()
Gets the raw data of the certificate.
Returns
A byte array containing the raw certificate data.
GetSerialNumberString()
Gets the serial number of the certificate as a string.
Returns
A string representing the serial number.
GetSignatureAlgorithm()
Gets the name of the signature algorithm used to sign the certificate.
Returns
A string representing the signature algorithm.
GetTextX509()
Gets a string representation of the certificate's information.
Returns
A string containing the certificate's details.
Import(byte[] rawData)
Imports a certificate from a byte array.
Parameters
- rawData: A byte array containing the certificate data.
Import(string fileName)
Imports a certificate from a file.
Parameters
- fileName: The path to the certificate file.
Import(string fileName, string password, X509KeyStorageFlags keyStorageFlags)
Imports a certificate from a file with specified password and key storage flags.
Parameters
- fileName: The path to the certificate file.
- password: The password for the certificate file.
- keyStorageFlags: Flags that control the import and storage of the private key.
Reset()
Resets the certificate information to its initial state.
Usage Example
Loading and Inspecting a Certificate
using System;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class CertificateExample
{
public static void Main(string[] args)
{
try
{
// Load a certificate from a file (replace with your certificate path)
string certificatePath = "path/to/your/certificate.pfx";
string certificatePassword = "your_password"; // If the certificate is password protected
X509Certificate2 cert = new X509Certificate2(certificatePath, certificatePassword);
Console.WriteLine("Certificate Loaded Successfully!");
Console.WriteLine($"Subject: {cert.Subject}");
Console.WriteLine($"Issuer: {cert.Issuer}");
Console.WriteLine($"Thumbprint: {cert.Thumbprint}");
Console.WriteLine($"Valid From: {cert.NotBefore}");
Console.WriteLine($"Valid To: {cert.NotAfter}");
Console.WriteLine($"Has Private Key: {cert.HasPrivateKey}");
Console.WriteLine($"Key Algorithm: {cert.KeyAlgorithm}");
Console.WriteLine($"Key Length: {cert.KeyLength} bits");
// You can also access extensions
foreach (X509Extension extension in cert.Extensions)
{
Console.WriteLine($"Extension: {extension.Oid.FriendlyName} ({extension.Oid.Value})");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading certificate: {ex.Message}");
}
}
}
The X509Certificate2 class is fundamental for implementing secure network communications in .NET, enabling robust authentication and encryption mechanisms.