.NET Framework Class Library
X509Certificate Class
Provides methods that help you use X.509 v.3 certificates.
Inheritance Hierarchy
System.Object
System.Security.Cryptography.X509Certificates.X509Certificate
System.Security.Cryptography.X509Certificates.X509Certificate2
Namespace: System.Security.Cryptography.X509Certificates
Assembly: mscorlib (in mscorlib.dll)
Syntax
Visual Basic
<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public Class X509Certificate _ Implements IDeserializationCallback, ISerializable
C#
[SerializableAttribute] [ComVisibleAttribute(true)] public class X509Certificate : IDeserializationCallback, ISerializable
Visual C++
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class X509Certificate : IDeserializationCallback, ISerializable
F#
[<SerializableAttribute>] [<ComVisibleAttribute(true)>] type X509Certificate = class interface IDeserializationCallback interface ISerializable end
The X509Certificate type exposes the following members.
Constructors
| Name | Description | |
|---|---|---|
|
X509Certificate() | Initializes a new instance of the X509Certificate class. |
|
X509Certificate(Byte[]) | Initializes a new instance of the X509Certificate class defined from a sequence of bytes representing an X.509v3 certificate. |
|
X509Certificate(IntPtr) | Initializes a new instance of the X509Certificate class using a handle to an unmanaged PCCERT_CONTEXT structure. |
|
X509Certificate(String) | Initializes a new instance of the X509Certificate class using the name of a PKCS7 signed file. |
|
X509Certificate(X509Certificate) | Initializes a new instance of the X509Certificate class using another X509Certificate class. |
|
X509Certificate(Byte[], SecureString) | Initializes a new instance of the X509Certificate class using a byte array and a password. |
|
X509Certificate(Byte[], String) | Initializes a new instance of the X509Certificate class using a byte array and a password. |
|
X509Certificate(SerializationInfo, StreamingContext) | Initializes a new instance of the X509Certificate class using a SerializationInfo object and a StreamingContext structure. |
|
X509Certificate(String, SecureString) | Initializes a new instance of the X509Certificate class using a certificate file name and a password. |
|
X509Certificate(String, String) | Initializes a new instance of the X509Certificate class using the name of a PKCS7 signed file and a password to access the certificate. |
|
X509Certificate(Byte[], SecureString, X509KeyStorageFlags) | Initializes a new instance of the X509Certificate class using a byte array, a password, and a key storage flag. |
|
X509Certificate(Byte[], String, X509KeyStorageFlags) | Initializes a new instance of the X509Certificate class using a byte array, a password, and a key storage flag. |
|
X509Certificate(String, SecureString, X509KeyStorageFlags) | Initializes a new instance of the X509Certificate class using a certificate file name, a password, and a key storage flag. |
|
X509Certificate(String, String, X509KeyStorageFlags) | Initializes a new instance of the X509Certificate class using the name of a PKCS7 signed file, a password to access the certificate, and a key storage flag. |
Properties
| Name | Description | |
|---|---|---|
|
Handle | Gets a handle to a Microsoft Cryptographic API certificate context described by an unmanaged PCCERT_CONTEXT structure. |
|
Issuer | Gets the name of the certificate authority that issued the X.509v3 certificate. |
|
Subject | Gets the subject distinguished name from the certificate. |
Methods
| Name | Description | |
|---|---|---|
|
CreateFromCertFile | Creates an X.509v3 certificate from the specified PKCS7 signed file. |
|
CreateFromSignedFile | Creates an X.509v3 certificate from the specified signed file. |
|
Equals(Object) | Compares two X509Certificate objects for equality. (Overrides Object.Equals(Object).) |
|
Equals(X509Certificate) | Compares two X509Certificate objects for equality. |
|
Export(X509ContentType) | Exports the current X509Certificate object to a byte array in a format described by one of the X509ContentType values. |
|
Export(X509ContentType, SecureString) | Exports the current X509Certificate object to a byte array using the specified format and a password. |
|
Export(X509ContentType, String) | Exports the current X509Certificate object to a byte array in a format described by one of the X509ContentType values, and using the specified password. |
|
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
|
FormatDate | Converts the specified date and time to a string. |
|
GetCertHash | Returns the hash value for the X.509v3 certificate as an array of bytes. |
|
GetCertHashString | Returns the SHA1 hash value for the X.509v3 certificate as a hexadecimal string. |
|
GetEffectiveDateString | Returns the effective date of this X.509v3 certificate. |
|
GetExpirationDateString | Returns the expiration date of this X.509v3 certificate. |
|
GetFormat | Returns the name of the format of this X.509v3 certificate. |
|
GetHashCode | Returns the hash code for the X.509v3 certificate as an integer. (Overrides Object.GetHashCode().) |
|
GetIssuerName | Obsolete. Returns the name of the certification authority that issued the X.509v3 certificate. |
|
GetKeyAlgorithm | Returns the key algorithm information for this X.509v3 certificate. |
|
GetKeyAlgorithmParameters | Returns the key algorithm parameters for the X.509v3 certificate. |
|
GetKeyAlgorithmParametersString | Returns the key algorithm parameters for the X.509v3 certificate. |
|
GetName | Obsolete. Returns the name of the principal to which the certificate was issued. |
|
GetPublicKey | Returns the public key for the X.509v3 certificate. |
|
GetPublicKeyString | Returns the public key for the X.509v3 certificate. |
|
GetRawCertData | Returns the raw data for the entire X.509v3 certificate. |
|
GetRawCertDataString | Returns the raw data for the entire X.509v3 certificate. |
|
GetSerialNumber | Returns the serial number of the X.509v3 certificate. |
|
GetSerialNumberString | Returns the serial number of the X.509v3 certificate. |
|
GetType | Gets the Type of the current instance. (Inherited from Object.) |
|
Import(Byte[]) | Populates the X509Certificate object with data from a byte array. |
|
Import(String) | Populates the X509Certificate object with information from a certificate file. |
|
Import(Byte[], SecureString, X509KeyStorageFlags) | Populates an X509Certificate object using data from a byte array, a password, and a key storage flag. |
|
Import(Byte[], String, X509KeyStorageFlags) | Populates the X509Certificate object using data from a byte array, a password, and flags for determining how the private key is imported. |
|
Import(String, SecureString, X509KeyStorageFlags) | Populates an X509Certificate object with information from a certificate file, a password, and a key storage flag. |
|
Import(String, String, X509KeyStorageFlags) | Populates the X509Certificate object with information from a certificate file, a password, and a X509KeyStorageFlags value. |
|
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
|
Reset | Resets the state of the X509Certificate2 object. |
|
ToString() | Returns a string representation of the current X509Certificate object. (Overrides Object.ToString().) |
|
ToString(Boolean) | Returns a string representation of the current X509Certificate object, with extra information, if specified. |
Explicit Interface Implementations
| Name | Description | |
|---|---|---|
|
IDeserializationCallback.OnDeserialization | Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete. |
|
ISerializable.GetObjectData | Gets serialization information with all the data needed to recreate an instance of the current X509Certificate object. |
Remarks
ASN.1 DER is the only certificate format supported by this class.
For most scenarios, you should use the X509Certificate2 class instead.
Examples
The following example loads an X.509 certificate from a file, calls the ToString method, and displays the results to the console.
Visual Basic
Imports System Imports System.Security.Cryptography.X509Certificates Module X509 Sub Main() ' The path to the certificate. Dim Certificate As String = "Certificate.cer" ' Load the certificate into an X509Certificate object. Dim cert As New X509Certificate(Certificate) ' Get the value. Dim resultsTrue As String = cert.ToString(True) ' Display the value to the console. Console.WriteLine(resultsTrue) ' Get the value. Dim resultsFalse As String = cert.ToString(False) ' Display the value to the console. Console.WriteLine(resultsFalse) End Sub End Module
C#
using System; using System.Security.Cryptography.X509Certificates; public class X509 { public static void Main() { // The path to the certificate. string Certificate = "Certificate.cer"; // Load the certificate into an X509Certificate object. X509Certificate cert = new X509Certificate(Certificate); // Get the value. string resultsTrue = cert.ToString(true); // Display the value to the console. Console.WriteLine(resultsTrue); // Get the value. string resultsFalse = cert.ToString(false); // Display the value to the console. Console.WriteLine(resultsFalse); } }
Visual C++
using namespace System; using namespace System::Security::Cryptography::X509Certificates; int main() { // The path to the certificate. String^ Certificate = L"Certificate.cer"; // Load the certificate into an X509Certificate object. X509Certificate^ cert = gcnew X509Certificate( Certificate ); // Get the value. String^ resultsTrue = cert->ToString( true ); // Display the value to the console. Console::WriteLine( resultsTrue ); // Get the value. String^ resultsFalse = cert->ToString( false ); // Display the value to the console. Console::WriteLine( resultsFalse ); }
Version Information
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.See Also