X509Certificate X509Certificate X509Certificate X509Certificate Class

Definition

Provides methods that help you use X.509 v.3 certificates.

public ref class X509Certificate : IDisposable, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
[Serializable]
public class X509Certificate : IDisposable, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type X509Certificate = class
    interface IDeserializationCallback
    interface ISerializable
    interface IDisposable
Public Class X509Certificate
Implements IDeserializationCallback, IDisposable, ISerializable
Inheritance
X509CertificateX509CertificateX509CertificateX509Certificate
Derived
Attributes
ComVisibleAttribute SerializableAttribute
Implements

Examples

The following example loads an X.509 certificate from a file, calls the ToString method, and displays the results to the console.

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 );
}

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);

    }

}
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

Remarks

ASN.1 DER is the only certificate format supported by this class.

For most scenarios, you should use the X509Certificate2 class instead.

Important

Starting with the .NET Framework 4.6, this type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

For apps that target the .NET Framework 4.5.2 and earlier versions, the X509Certificate class does not implement the IDisposable interface and therefore does not have a Dispose method.

Constructors

X509Certificate() X509Certificate() X509Certificate() X509Certificate()

Initializes a new instance of the X509Certificate class.

X509Certificate(Byte[]) X509Certificate(Byte[]) X509Certificate(Byte[]) X509Certificate(Byte[])

Initializes a new instance of the X509Certificate class defined from a sequence of bytes representing an X.509v3 certificate.

X509Certificate(Byte[], SecureString) X509Certificate(Byte[], SecureString) X509Certificate(Byte[], SecureString) X509Certificate(Byte[], SecureString)

Initializes a new instance of the X509Certificate class using a byte array and a password.

X509Certificate(Byte[], SecureString, X509KeyStorageFlags) X509Certificate(Byte[], SecureString, X509KeyStorageFlags) X509Certificate(Byte[], SecureString, X509KeyStorageFlags) 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) X509Certificate(Byte[], String) X509Certificate(Byte[], String) X509Certificate(Byte[], String)

Initializes a new instance of the X509Certificate class using a byte array and a password.

X509Certificate(Byte[], String, X509KeyStorageFlags) X509Certificate(Byte[], String, X509KeyStorageFlags) X509Certificate(Byte[], String, X509KeyStorageFlags) X509Certificate(Byte[], String, X509KeyStorageFlags)

Initializes a new instance of the X509Certificate class using a byte array, a password, and a key storage flag.

X509Certificate(IntPtr) X509Certificate(IntPtr) X509Certificate(IntPtr) X509Certificate(IntPtr)

Initializes a new instance of the X509Certificate class using a handle to an unmanaged PCCERT_CONTEXT structure.

X509Certificate(SerializationInfo, StreamingContext) X509Certificate(SerializationInfo, StreamingContext) X509Certificate(SerializationInfo, StreamingContext) X509Certificate(SerializationInfo, StreamingContext)

Initializes a new instance of the X509Certificate class using a SerializationInfo object and a StreamingContext structure.

X509Certificate(String) X509Certificate(String) X509Certificate(String) X509Certificate(String)

Initializes a new instance of the X509Certificate class using the name of a PKCS7 signed file.

X509Certificate(String, SecureString) X509Certificate(String, SecureString) X509Certificate(String, SecureString) X509Certificate(String, SecureString)

Initializes a new instance of the X509Certificate class using a certificate file name and a password.

X509Certificate(String, SecureString, X509KeyStorageFlags) X509Certificate(String, SecureString, X509KeyStorageFlags) X509Certificate(String, SecureString, X509KeyStorageFlags) 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) X509Certificate(String, String) X509Certificate(String, String) 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(String, String, X509KeyStorageFlags) X509Certificate(String, String, X509KeyStorageFlags) X509Certificate(String, String, X509KeyStorageFlags) 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.

X509Certificate(X509Certificate) X509Certificate(X509Certificate) X509Certificate(X509Certificate) X509Certificate(X509Certificate)

Initializes a new instance of the X509Certificate class using another X509Certificate class.

Properties

Handle Handle Handle Handle

Gets a handle to a Microsoft Cryptographic API certificate context described by an unmanaged PCCERT_CONTEXT structure.

Issuer Issuer Issuer Issuer

Gets the name of the certificate authority that issued the X.509v3 certificate.

Subject Subject Subject Subject

Gets the subject distinguished name from the certificate.

Methods

CreateFromCertFile(String) CreateFromCertFile(String) CreateFromCertFile(String) CreateFromCertFile(String)

Creates an X.509v3 certificate from the specified PKCS7 signed file.

CreateFromSignedFile(String) CreateFromSignedFile(String) CreateFromSignedFile(String) CreateFromSignedFile(String)

Creates an X.509v3 certificate from the specified signed file.

Dispose() Dispose() Dispose() Dispose()

Releases all resources used by the current X509Certificate object.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

Releases all of the unmanaged resources used by this X509Certificate and optionally releases the managed resources.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Compares two X509Certificate objects for equality.

Equals(X509Certificate) Equals(X509Certificate) Equals(X509Certificate) Equals(X509Certificate)

Compares two X509Certificate objects for equality.

Export(X509ContentType) Export(X509ContentType) Export(X509ContentType) Export(X509ContentType)

Exports the current X509Certificate object to a byte array in a format described by one of the X509ContentType values.

Export(X509ContentType, SecureString) Export(X509ContentType, SecureString) Export(X509ContentType, SecureString) Export(X509ContentType, SecureString)

Exports the current X509Certificate object to a byte array using the specified format and a password.

Export(X509ContentType, String) Export(X509ContentType, String) Export(X509ContentType, String) 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.

FormatDate(DateTime) FormatDate(DateTime) FormatDate(DateTime) FormatDate(DateTime)

Converts the specified date and time to a string.

GetCertHash() GetCertHash() GetCertHash() GetCertHash()

Returns the hash value for the X.509v3 certificate as an array of bytes.

GetCertHash(HashAlgorithmName) GetCertHash(HashAlgorithmName) GetCertHash(HashAlgorithmName) GetCertHash(HashAlgorithmName)
GetCertHashString() GetCertHashString() GetCertHashString() GetCertHashString()

Returns the SHA1 hash value for the X.509v3 certificate as a hexadecimal string.

GetCertHashString(HashAlgorithmName) GetCertHashString(HashAlgorithmName) GetCertHashString(HashAlgorithmName) GetCertHashString(HashAlgorithmName)
GetEffectiveDateString() GetEffectiveDateString() GetEffectiveDateString() GetEffectiveDateString()

Returns the effective date of this X.509v3 certificate.

GetExpirationDateString() GetExpirationDateString() GetExpirationDateString() GetExpirationDateString()

Returns the expiration date of this X.509v3 certificate.

GetFormat() GetFormat() GetFormat() GetFormat()

Returns the name of the format of this X.509v3 certificate.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Returns the hash code for the X.509v3 certificate as an integer.

GetIssuerName() GetIssuerName() GetIssuerName() GetIssuerName()

Returns the name of the certification authority that issued the X.509v3 certificate.

GetKeyAlgorithm() GetKeyAlgorithm() GetKeyAlgorithm() GetKeyAlgorithm()

Returns the key algorithm information for this X.509v3 certificate as a string.

GetKeyAlgorithmParameters() GetKeyAlgorithmParameters() GetKeyAlgorithmParameters() GetKeyAlgorithmParameters()

Returns the key algorithm parameters for the X.509v3 certificate as an array of bytes.

GetKeyAlgorithmParametersString() GetKeyAlgorithmParametersString() GetKeyAlgorithmParametersString() GetKeyAlgorithmParametersString()

Returns the key algorithm parameters for the X.509v3 certificate as a hexadecimal string.

GetName() GetName() GetName() GetName()

Returns the name of the principal to which the certificate was issued.

GetPublicKey() GetPublicKey() GetPublicKey() GetPublicKey()

Returns the public key for the X.509v3 certificate as an array of bytes.

GetPublicKeyString() GetPublicKeyString() GetPublicKeyString() GetPublicKeyString()

Returns the public key for the X.509v3 certificate as a hexadecimal string.

GetRawCertData() GetRawCertData() GetRawCertData() GetRawCertData()

Returns the raw data for the entire X.509v3 certificate as an array of bytes.

GetRawCertDataString() GetRawCertDataString() GetRawCertDataString() GetRawCertDataString()

Returns the raw data for the entire X.509v3 certificate as a hexadecimal string.

GetSerialNumber() GetSerialNumber() GetSerialNumber() GetSerialNumber()

Returns the serial number of the X.509v3 certificate as an array of bytes in little-endian order.

GetSerialNumberString() GetSerialNumberString() GetSerialNumberString() GetSerialNumberString()

Returns the serial number of the X.509v3 certificate as a little-endian hexadecimal string .

GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
Import(Byte[]) Import(Byte[]) Import(Byte[]) Import(Byte[])

Populates the X509Certificate object with data from a byte array.

Import(Byte[], SecureString, X509KeyStorageFlags) Import(Byte[], SecureString, X509KeyStorageFlags) Import(Byte[], SecureString, X509KeyStorageFlags) 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) Import(Byte[], String, X509KeyStorageFlags) Import(Byte[], String, X509KeyStorageFlags) 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) Import(String) Import(String) Import(String)

Populates the X509Certificate object with information from a certificate file.

Import(String, SecureString, X509KeyStorageFlags) Import(String, SecureString, X509KeyStorageFlags) Import(String, SecureString, X509KeyStorageFlags) 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) Import(String, String, X509KeyStorageFlags) Import(String, String, X509KeyStorageFlags) Import(String, String, X509KeyStorageFlags)

Populates the X509Certificate object with information from a certificate file, a password, and a X509KeyStorageFlags value.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Reset() Reset() Reset() Reset()

Resets the state of the X509Certificate2 object.

ToString() ToString() ToString() ToString()

Returns a string representation of the current X509Certificate object.

ToString(Boolean) ToString(Boolean) ToString(Boolean) ToString(Boolean)

Returns a string representation of the current X509Certificate object, with extra information, if specified.

TryGetCertHash(HashAlgorithmName, Span<Byte>, Int32) TryGetCertHash(HashAlgorithmName, Span<Byte>, Int32) TryGetCertHash(HashAlgorithmName, Span<Byte>, Int32) TryGetCertHash(HashAlgorithmName, Span<Byte>, Int32)

Explicit Interface Implementations

IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object)

Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete.

ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Gets serialization information with all the data needed to recreate an instance of the current X509Certificate object.

Applies to