X509ContentType Enumeration
Specifies the content type of an X.509 certificate.
Namespace:
Assembly:
System.Security.Cryptography.dll
Members
The X509ContentType
enumeration defines the following members:
Member | Description |
---|---|
Cert | The certificate is an X.509 certificate. |
Pkcs12 | The certificate is a PKCS #12 formatted certificate (PFX file). This format includes the public key, private key, and certificate chain. |
Pkcs7 | The certificate is a PKCS #7 formatted certificate. This format typically contains a certificate chain but not a private key. |
Unknown | The content type is unknown or not specified. |
Remarks
The X509ContentType
enumeration is used by the X509Certificate2 constructor to specify the format of the data used to initialize a new instance of the X509Certificate2
class. This allows the system to correctly parse and interpret the provided certificate data, whether it's a standalone certificate, a PKCS #12 (PFX) file, or a PKCS #7 certificate collection.
When working with certificate files, it's important to know the format to ensure proper loading and handling of private keys and certificate chains.
Example
The following code example demonstrates how to use the X509ContentType
enumeration when creating an X509Certificate2
object from a PFX file.
using System;
using System.Security.Cryptography.X509Certificates;
public class CertificateLoader {
public static void LoadPfxCertificate(string pfxFilePath, string password) {
try {
X509Certificate2 cert = new X509Certificate2(pfxFilePath, password, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
Console.WriteLine($"Successfully loaded certificate: {cert.Subject}");
// You can now use the 'cert' object for cryptographic operations
} catch (CryptographicException ex) {
Console.WriteLine($"Error loading certificate: {ex.Message}");
}
}
}
In this example, the X509Certificate2
constructor is called with the file path, password, and appropriate key storage flags. The X509ContentType.Pkcs12
is implicitly understood by the constructor when dealing with PFX files, but specifying it explicitly can sometimes be useful in more complex scenarios or when reading raw byte arrays.