X509Certificate2 Constructor
System.Net.Security
Initializes a new instance of the X509Certificate2 class.
Public Constructors
X509Certificate2()
public X509Certificate2()
Initializes a new, empty instance of the X509Certificate2 class.
X509Certificate2(byte[] rawData)
public X509Certificate2(byte[] rawData)
Initializes a new instance of the X509Certificate2 class with the specified data.
Parameters
rawData: A byte array that contains the encoded certificate.
X509Certificate2(byte[], string) or X509Certificate2(byte[], SecureString) constructor.X509Certificate2(byte[] rawData, string password)
public X509Certificate2(byte[] rawData, string password)
Initializes a new instance of the X509Certificate2 class with the specified data and password.
Parameters
rawData: A byte array that contains the encoded certificate.password: The password to open the certificate.
X509Certificate2(byte[], SecureString) constructor.X509Certificate2(byte[] rawData, SecureString password)
public X509Certificate2(byte[] rawData, SecureString password)
Initializes a new instance of the X509Certificate2 class with the specified data and password, using the default cryptographic service provider (CSP).
Parameters
rawData: A byte array that contains the encoded certificate.password: A SecureString object that contains the password to open the certificate.
X509Certificate2(byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
public X509Certificate2(byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
Initializes a new instance of the X509Certificate2 class with the specified data, password, and key storage options.
Parameters
rawData: A byte array that contains the encoded certificate.password: A SecureString object that contains the password to open the certificate.keyStorageFlags: A bitwise combination of enumeration values that specifies how to import the certificate and its associated private key.
X509Certificate2(string fileName)
public X509Certificate2(string fileName)
Initializes a new instance of the X509Certificate2 class from the specified file.
Parameters
fileName: The path to the certificate file.
X509Certificate2(string, string) or X509Certificate2(string, SecureString) constructor.X509Certificate2(string fileName, string password)
public X509Certificate2(string fileName, string password)
Initializes a new instance of the X509Certificate2 class from the specified file and password.
Parameters
fileName: The path to the certificate file.password: The password to open the certificate.
X509Certificate2(string, SecureString) constructor.X509Certificate2(string fileName, SecureString password)
public X509Certificate2(string fileName, SecureString password)
Initializes a new instance of the X509Certificate2 class from the specified file and password, using the default cryptographic service provider (CSP).
Parameters
fileName: The path to the certificate file.password: A SecureString object that contains the password to open the certificate.
X509Certificate2(string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
public X509Certificate2(string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
Initializes a new instance of the X509Certificate2 class from the specified file, password, and key storage options.
Parameters
fileName: The path to the certificate file.password: A SecureString object that contains the password to open the certificate.keyStorageFlags: A bitwise combination of enumeration values that specifies how to import the certificate and its associated private key.
X509Certificate2(X509Certificate certificate)
public X509Certificate2(X509Certificate certificate)
Initializes a new instance of the X509Certificate2 class from an existing X509Certificate object.
Parameters
certificate: An existing X509Certificate object.
X509Certificate2(X509Certificate certificate, string password)
public X509Certificate2(X509Certificate certificate, string password)
Initializes a new instance of the X509Certificate2 class from an existing X509Certificate object and password.
Parameters
certificate: An existing X509Certificate object.password: The password to open the certificate.
X509Certificate2(X509Certificate certificate, SecureString password)
public X509Certificate2(X509Certificate certificate, SecureString password)
Initializes a new instance of the X509Certificate2 class from an existing X509Certificate object and password, using the default cryptographic service provider (CSP).
Parameters
certificate: An existing X509Certificate object.password: A SecureString object that contains the password to open the certificate.
X509Certificate2(X509Certificate certificate, SecureString password, X509KeyStorageFlags keyStorageFlags)
public X509Certificate2(X509Certificate certificate, SecureString password, X509KeyStorageFlags keyStorageFlags)
Initializes a new instance of the X509Certificate2 class from an existing X509Certificate object, password, and key storage options.
Parameters
certificate: An existing X509Certificate object.password: A SecureString object that contains the password to open the certificate.keyStorageFlags: A bitwise combination of enumeration values that specifies how to import the certificate and its associated private key.
The X509Certificate2 class represents an X.509 certificate, which is a digital certificate that uses the X.509 public key infrastructure (PKI) standard. This class provides access to the certificate's properties, such as its subject, issuer, expiration date, and public key.
When initializing an X509Certificate2 object from a file or byte array that contains a private key, you may need to provide a password. The password is used to decrypt the private key.
The X509KeyStorageFlags enumeration allows you to control how the private key is imported and stored. For example, you can specify whether the private key should be exportable, whether it should be stored in the user's certificate store or the machine's certificate store, and whether it should be associated with the certificate.
The following example demonstrates how to create an X509Certificate2 object from a file and access its properties.
using System;
using System.Net.Security;
public class Example
{
public static void Main(string[] args)
{
try
{
// Replace "path/to/your/certificate.pfx" with the actual path to your PFX file
// Replace "your_password" with the password for your PFX file
string certificatePath = "path/to/your/certificate.pfx";
string certificatePassword = "your_password";
X509Certificate2 cert = new X509Certificate2(certificatePath, certificatePassword);
Console.WriteLine($"Certificate Name: {cert.SubjectName.Name}");
Console.WriteLine($"Issuer: {cert.IssuerName.Name}");
Console.WriteLine($"Expires on: {cert.NotAfter}");
Console.WriteLine($"Has private key: {cert.HasPrivateKey}");
// You can now use the 'cert' object for SSL/TLS operations or other cryptographic tasks.
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}