Initializes a new instance of the X509Certificate2 class.
The X509Certificate2 class represents a public key certificate that conforms to the X.509 standard. This class provides access to the certificate's public key, private key (if available), and other properties. The constructors allow you to load certificate data from various sources, such as byte arrays or file paths.
X509Certificate2()Initializes a new, empty instance of the X509Certificate2 class.
public X509Certificate2();
X509Certificate2(IntPtr handle)Initializes a new instance of the X509Certificate2 class using a handle to an unmanaged certificate.
Parameters:
| Name | Type | Description |
|---|---|---|
handle |
IntPtr | A handle to an unmanaged certificate. |
public X509Certificate2(IntPtr handle);
X509Certificate2(Byte[] rawData)Initializes a new instance of the X509Certificate2 class from a byte array.
Parameters:
| Name | Type | Description |
|---|---|---|
rawData |
Byte[] | A byte array containing the DER-encoded X.509 certificate. |
public X509Certificate2(byte[] rawData);
X509Certificate2(String fileName)Initializes a new instance of the X509Certificate2 class from a file.
Parameters:
| Name | Type | Description |
|---|---|---|
fileName |
String | The name of the file containing the X.509 certificate. This can be a certificate file (e.g., .cer, .crt, .pfx) or a portable executable file. |
public X509Certificate2(string fileName);
X509Certificate2(String fileName, String password)Initializes a new instance of the X509Certificate2 class from a file and specifies the password to open the certificate.
Parameters:
| Name | Type | Description |
|---|---|---|
fileName |
String | The name of the file containing the X.509 certificate. This is typically a .pfx file. |
password |
String | The password required to open the certificate file. |
public X509Certificate2(string fileName, string password);
X509Certificate2(String fileName, String password, X509KeyStorageFlags keyStorageFlags)Initializes a new instance of the X509Certificate2 class from a file, specifying the password and key storage options.
Parameters:
| Name | Type | Description |
|---|---|---|
fileName |
String | The name of the file containing the X.509 certificate. This is typically a .pfx file. |
password |
String | The password required to open the certificate file. |
keyStorageFlags |
X509KeyStorageFlags | One of the X509KeyStorageFlags values that specifies how the private key is stored or accessed. |
public X509Certificate2(string fileName, string password, X509KeyStorageFlags keyStorageFlags);
X509Certificate2(Byte[] rawData, String password)Initializes a new instance of the X509Certificate2 class from a byte array, specifying the password.
Parameters:
| Name | Type | Description |
|---|---|---|
rawData |
Byte[] | A byte array containing the DER-encoded or PKCS#12 encoded X.509 certificate. |
password |
String | The password required to decrypt the certificate if it's PKCS#12 encoded. |
public X509Certificate2(byte[] rawData, string password);
X509Certificate2(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags)Initializes a new instance of the X509Certificate2 class from a byte array, specifying the password and key storage options.
Parameters:
| Name | Type | Description |
|---|---|---|
rawData |
Byte[] | A byte array containing the DER-encoded or PKCS#12 encoded X.509 certificate. |
password |
String | The password required to decrypt the certificate if it's PKCS#12 encoded. |
keyStorageFlags |
X509KeyStorageFlags | One of the X509KeyStorageFlags values that specifies how the private key is stored or accessed. |
public X509Certificate2(byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags);
When initializing an X509Certificate2 object from a file or byte array that contains a private key (e.g., a PKCS#12 file), the HasPrivateKey property will be set to true. If the certificate does not contain a private key, this property will be false.
The X509KeyStorageFlags enumeration controls how the private key associated with the certificate is handled. This includes options for storing the key in a user-specific or machine-specific store, or exporting the key.
Note: When loading certificates from a file or byte array, ensure that the data is correctly formatted and that the provided password (if any) is accurate. Incorrect data or passwords will result in exceptions.
Important: Handling private keys requires careful security considerations. Avoid storing sensitive private keys in easily accessible locations and use strong passwords. For production environments, consider using secure key management solutions.