X509Certificate Class (System.Security.Cryptography.X509Certificates)

Switch View :
ScriptFree
.NET Framework Class Library
X509Certificate Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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
Public method X509Certificate() Initializes a new instance of the X509Certificate class.
Public method X509Certificate(Byte[]) Initializes a new instance of the X509Certificate class defined from a sequence of bytes representing an X.509v3 certificate.
Public method X509Certificate(IntPtr) Initializes a new instance of the X509Certificate class using a handle to an unmanaged PCCERT_CONTEXT structure.
Public method X509Certificate(String) Initializes a new instance of the X509Certificate class using the name of a PKCS7 signed file.
Public method X509Certificate(X509Certificate) Initializes a new instance of the X509Certificate class using another X509Certificate class.
Public method X509Certificate(Byte[], SecureString) Initializes a new instance of the X509Certificate class using a byte array and a password.
Public method X509Certificate(Byte[], String) Initializes a new instance of the X509Certificate class using a byte array and a password.
Public method X509Certificate(SerializationInfo, StreamingContext) Initializes a new instance of the X509Certificate class using a SerializationInfo object and a StreamingContext structure.
Public method X509Certificate(String, SecureString) Initializes a new instance of the X509Certificate class using a certificate file name and a password.
Public method 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.
Public method X509Certificate(Byte[], SecureString, X509KeyStorageFlags) Initializes a new instance of the X509Certificate class using a byte array, a password, and a key storage flag.
Public method X509Certificate(Byte[], String, X509KeyStorageFlags) Initializes a new instance of the X509Certificate class using a byte array, a password, and a key storage flag.
Public method X509Certificate(String, SecureString, X509KeyStorageFlags) Initializes a new instance of the X509Certificate class using a certificate file name, a password, and a key storage flag.
Public method 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.
Top
Properties

  Name Description
Public property Handle Gets a handle to a Microsoft Cryptographic API certificate context described by an unmanaged PCCERT_CONTEXT structure.
Public property Issuer Gets the name of the certificate authority that issued the X.509v3 certificate.
Public property Subject Gets the subject distinguished name from the certificate.
Top
Methods

  Name Description
Public method Static member CreateFromCertFile Creates an X.509v3 certificate from the specified PKCS7 signed file.
Public method Static member CreateFromSignedFile Creates an X.509v3 certificate from the specified signed file.
Public method Equals(Object) Compares two X509Certificate objects for equality. (Overrides Object.Equals(Object).)
Public method Equals(X509Certificate) Compares two X509Certificate objects for equality.
Public method Export(X509ContentType) Exports the current X509Certificate object to a byte array in a format described by one of the X509ContentType values.
Public method Export(X509ContentType, SecureString) Exports the current X509Certificate object to a byte array using the specified format and a password.
Public method 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.
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected method Static member FormatDate Converts the specified date and time to a string.
Public method GetCertHash Returns the hash value for the X.509v3 certificate as an array of bytes.
Public method GetCertHashString Returns the SHA1 hash value for the X.509v3 certificate as a hexadecimal string.
Public method GetEffectiveDateString Returns the effective date of this X.509v3 certificate.
Public method GetExpirationDateString Returns the expiration date of this X.509v3 certificate.
Public method GetFormat Returns the name of the format of this X.509v3 certificate.
Public method GetHashCode Returns the hash code for the X.509v3 certificate as an integer. (Overrides Object.GetHashCode().)
Public method GetIssuerName Obsolete. Returns the name of the certification authority that issued the X.509v3 certificate.
Public method GetKeyAlgorithm Returns the key algorithm information for this X.509v3 certificate.
Public method GetKeyAlgorithmParameters Returns the key algorithm parameters for the X.509v3 certificate.
Public method GetKeyAlgorithmParametersString Returns the key algorithm parameters for the X.509v3 certificate.
Public method GetName Obsolete. Returns the name of the principal to which the certificate was issued.
Public method GetPublicKey Returns the public key for the X.509v3 certificate.
Public method GetPublicKeyString Returns the public key for the X.509v3 certificate.
Public method GetRawCertData Returns the raw data for the entire X.509v3 certificate.
Public method GetRawCertDataString Returns the raw data for the entire X.509v3 certificate.
Public method GetSerialNumber Returns the serial number of the X.509v3 certificate.
Public method GetSerialNumberString Returns the serial number of the X.509v3 certificate.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Import(Byte[]) Populates the X509Certificate object with data from a byte array.
Public method Import(String) Populates the X509Certificate object with information from a certificate file.
Public method Import(Byte[], SecureString, X509KeyStorageFlags) Populates an X509Certificate object using data from a byte array, a password, and a key storage flag.
Public method 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.
Public method Import(String, SecureString, X509KeyStorageFlags) Populates an X509Certificate object with information from a certificate file, a password, and a key storage flag.
Public method Import(String, String, X509KeyStorageFlags) Populates the X509Certificate object with information from a certificate file, a password, and a X509KeyStorageFlags value.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Reset Resets the state of the X509Certificate2 object.
Public method ToString() Returns a string representation of the current X509Certificate object. (Overrides Object.ToString().)
Public method ToString(Boolean) Returns a string representation of the current X509Certificate object, with extra information, if specified.
Top
Explicit Interface Implementations

  Name Description
Explicit interface implemetation Private method IDeserializationCallback.OnDeserialization Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete.
Explicit interface implemetation Private method ISerializable.GetObjectData Gets serialization information with all the data needed to recreate an instance of the current X509Certificate object.
Top
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.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8 Consumer Preview, Windows Server 8 Beta, Windows 7, Windows Server 2008 SP2, Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Reference