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
public ref class X509Certificate : IDisposable
public ref class X509Certificate
public ref class X509Certificate : System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
public class X509Certificate : IDisposable, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
public class X509Certificate : IDisposable
[System.Serializable]
public class X509Certificate
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class X509Certificate : System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class X509Certificate : IDisposable, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type X509Certificate = class
    interface IDisposable
    interface IDeserializationCallback
    interface ISerializable
type X509Certificate = class
    interface IDisposable
type X509Certificate = class
    interface IDisposable
    interface ISerializable
    interface IDeserializationCallback
[<System.Serializable>]
type X509Certificate = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type X509Certificate = class
    interface IDeserializationCallback
    interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type X509Certificate = class
    interface IDisposable
    interface IDeserializationCallback
    interface ISerializable
Public Class X509Certificate
Implements IDeserializationCallback, IDisposable, ISerializable
Public Class X509Certificate
Implements IDisposable
Public Class X509Certificate
Public Class X509Certificate
Implements IDeserializationCallback, ISerializable
Inheritance
X509Certificate
Derived
Attributes
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.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()
Obsolete.

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

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

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)

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

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)

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

X509Certificate(SerializationInfo, StreamingContext)
Obsolete.

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

X509Certificate(String)

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

X509Certificate(String, SecureString)

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

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)

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)

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)

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

Properties

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.

SerialNumberBytes

Gets the big-endian representation of the certificate's serial number.

Subject

Gets the subject distinguished name from the certificate.

Methods

CreateFromCertFile(String)

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

CreateFromSignedFile(String)

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

Dispose()

Releases all resources used by the current X509Certificate object.

Dispose(Boolean)

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

Equals(Object)

Compares two X509Certificate objects for equality.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from 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.

FormatDate(DateTime)

Converts the specified date and time to a string.

GetCertHash()

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

GetCertHash(HashAlgorithmName)

Returns the hash value for the X.509v3 certificate that is computed by using the specified cryptographic hash algorithm.

GetCertHashString()

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

GetCertHashString(HashAlgorithmName)

Returns a hexadecimal string containing the hash value for the X.509v3 certificate computed using the specified cryptographic hash algorithm.

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.

GetIssuerName()
Obsolete.
Obsolete.
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 as a string.

GetKeyAlgorithmParameters()

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

GetKeyAlgorithmParametersString()

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

GetName()
Obsolete.
Obsolete.
Obsolete.

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

GetPublicKey()

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

GetPublicKeyString()

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

GetRawCertData()

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

GetRawCertDataString()

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

GetSerialNumber()

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

GetSerialNumberString()

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

GetType()

Gets the Type of the current instance.

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

Populates the X509Certificate object with data from a byte array.

Import(Byte[], SecureString, X509KeyStorageFlags)
Obsolete.

Populates an X509Certificate object using data from a byte array, a password, and a key storage flag.

Import(Byte[], String, X509KeyStorageFlags)
Obsolete.

Populates the X509Certificate object using data from a byte array, a password, and flags for determining how the private key is imported.

Import(String)
Obsolete.

Populates the X509Certificate object with information from a certificate file.

Import(String, SecureString, X509KeyStorageFlags)
Obsolete.

Populates an X509Certificate object with information from a certificate file, a password, and a key storage flag.

Import(String, String, X509KeyStorageFlags)
Obsolete.

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.

ToString(Boolean)

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

TryGetCertHash(HashAlgorithmName, Span<Byte>, Int32)

Attempts to produce a "thumbprint" for the certificate by hashing the encoded representation of the certificate with the specified hash algorithm.

Explicit Interface Implementations

IDeserializationCallback.OnDeserialization(Object)

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

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

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

Applies to