X509Certificate 類別

定義

提供協助您使用 X.509 v.3 憑證的方法。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)]
[System.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
繼承
X509Certificate
衍生
屬性
實作

範例

下列範例會從檔案載入 x.509 憑證、呼叫 ToString 方法,並將結果顯示在主控台中。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

備註

Asn.1 DER 是此類別唯一支援的憑證格式。ASN.1 DER is the only certificate format supported by this class.

在大部分的情況下,您應該改用 X509Certificate2 類別。For most scenarios, you should use the X509Certificate2 class instead.

重要

.NET Framework 4.6.NET Framework 4.6開始,這個型別會執行 IDisposable 介面。Starting with the .NET Framework 4.6.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. 若要直接處置型別,請呼叫其 try/catch 區塊中的 Dispose 方法。To dispose of the type directly, call its Dispose method in a try/catch block. 若要間接處置它,請使用語言建構函式,例如 using (在 C# 中) 或 Using (在 Visual Basic 中)。To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). 如需詳細資訊,請參閱 IDisposable 介面文章中的<使用實作 IDisposable 的物件>一節。For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

針對以 .NET Framework 4.5.2.NET Framework 4.5.2 和較早版本為目標的應用程式,X509Certificate 類別不會執行 IDisposable 介面,因此不會有 Dispose 方法。For apps that target the .NET Framework 4.5.2.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.

建構函式

X509Certificate()

初始化 X509Certificate 類別的新執行個體。Initializes a new instance of the X509Certificate class.

X509Certificate(Byte[])

初始化 X509Certificate 類別的新執行個體 (這個類別是在表示 X.509v3 憑證的位元組序列中定義的)。Initializes a new instance of the X509Certificate class defined from a sequence of bytes representing an X.509v3 certificate.

X509Certificate(Byte[], SecureString)

使用位元組陣列和密碼,初始化 X509Certificate 類別的新執行個體。Initializes a new instance of the X509Certificate class using a byte array and a password.

X509Certificate(Byte[], SecureString, X509KeyStorageFlags)

使用位元組陣列、密碼和金鑰儲存旗標,初始化 X509Certificate 類別的新執行個體。Initializes a new instance of the X509Certificate class using a byte array, a password, and a key storage flag.

X509Certificate(Byte[], String)

使用位元組陣列和密碼,初始化 X509Certificate 類別的新執行個體。Initializes a new instance of the X509Certificate class using a byte array and a password.

X509Certificate(Byte[], String, X509KeyStorageFlags)

使用位元組陣列、密碼和金鑰儲存旗標,初始化 X509Certificate 類別的新執行個體。Initializes a new instance of the X509Certificate class using a byte array, a password, and a key storage flag.

X509Certificate(IntPtr)

使用 Unmanaged PCCERT_CONTEXT 結構的控制代碼,初始化 X509Certificate 類別的新執行個體。Initializes a new instance of the X509Certificate class using a handle to an unmanaged PCCERT_CONTEXT structure.

X509Certificate(SerializationInfo, StreamingContext)

使用 X509Certificate 物件和 SerializationInfo 結構,初始化 StreamingContext 類別的新執行個體。Initializes a new instance of the X509Certificate class using a SerializationInfo object and a StreamingContext structure.

X509Certificate(String)

使用 PKCS7 已簽署檔案的名稱,初始化 X509Certificate 類別的新執行個體。Initializes a new instance of the X509Certificate class using the name of a PKCS7 signed file.

X509Certificate(String, SecureString)

使用憑證檔名和密碼,初始化 X509Certificate 類別的新執行個體。Initializes a new instance of the X509Certificate class using a certificate file name and a password.

X509Certificate(String, SecureString, X509KeyStorageFlags)

使用憑證檔名、密碼和金鑰儲存旗標,初始化 X509Certificate 類別的新執行個體。Initializes a new instance of the X509Certificate class using a certificate file name, a password, and a key storage flag.

X509Certificate(String, String)

使用 PKCS7 已簽署檔案和用於存取憑證的密碼,初始化 X509Certificate 類別的新執行個體。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)

使用 PKCS7 已簽署檔案、用於存取憑證的密碼和金鑰儲存旗標,初始化 X509Certificate 類別的新執行個體。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 類別的新執行個體。Initializes a new instance of the X509Certificate class using another X509Certificate class.

屬性

Handle

取得 Unmanaged PCCERT_CONTEXT 結構所描述之 Microsoft Cryptographic API 憑證內容的控制代碼。Gets a handle to a Microsoft Cryptographic API certificate context described by an unmanaged PCCERT_CONTEXT structure.

Issuer

取得核發 X.509v3 憑證的憑證授權單位名稱。Gets the name of the certificate authority that issued the X.509v3 certificate.

Subject

取得憑證的主旨辨別名稱。Gets the subject distinguished name from the certificate.

方法

CreateFromCertFile(String)

從指定的 PKCS7 已簽署檔案建立 X.509v3 憑證。Creates an X.509v3 certificate from the specified PKCS7 signed file.

CreateFromSignedFile(String)

從指定簽名檔建立 X.509v3 憑證。Creates an X.509v3 certificate from the specified signed file.

Dispose()

釋放由 X509Certificate 物件使用的所有資源。Releases all resources used by the current X509Certificate object.

Dispose(Boolean)

釋放這個 X509Certificate 使用的所有 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases all of the unmanaged resources used by this X509Certificate and optionally releases the managed resources.

Equals(Object)

比較兩個 X509Certificate 物件是否相等。Compares two X509Certificate objects for equality.

Equals(X509Certificate)

比較兩個 X509Certificate 物件是否相等。Compares two X509Certificate objects for equality.

Export(X509ContentType)

以其中一個 X509Certificate 值所描述的格式,將目前的 X509ContentType 物件匯出至位元組陣列。Exports the current X509Certificate object to a byte array in a format described by one of the X509ContentType values.

Export(X509ContentType, SecureString)

使用指定的格式和密碼,將目前的 X509Certificate 物件匯出至位元組陣列。Exports the current X509Certificate object to a byte array using the specified format and a password.

Export(X509ContentType, String)

以其中一個 X509Certificate 值所描述的格式,並使用指定的密碼,將目前的 X509ContentType 物件匯出至位元組陣列。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()

將 X.509v3 憑證的雜湊值 (Hash Value) 傳回為位元組陣列。Returns the hash value for the X.509v3 certificate as an array of bytes.

GetCertHash(HashAlgorithmName)

傳回使用指定密碼編譯雜湊演算法所計算 X.509v3 憑證的雜湊值。Returns the hash value for the X.509v3 certificate that is computed by using the specified cryptographic hash algorithm.

GetCertHashString()

將 X.509v3 憑證的 SHA1 雜湊值傳回為十六進位的字串。Returns the SHA1 hash value for the X.509v3 certificate as a hexadecimal string.

GetCertHashString(HashAlgorithmName)

傳回包含使用指定密碼編譯雜湊演算法所計算 X.509v3 憑證雜湊值的十六進位字串。Returns a hexadecimal string containing the hash value for the X.509v3 certificate computed using the specified cryptographic hash algorithm.

GetEffectiveDateString()

傳回這個 X.509v3 憑證的有效日期。Returns the effective date of this X.509v3 certificate.

GetExpirationDateString()

傳回這個 X.509v3 憑證的到期日。Returns the expiration date of this X.509v3 certificate.

GetFormat()

傳回這個 X.509v3 憑證的格式名稱。Returns the name of the format of this X.509v3 certificate.

GetHashCode()

將 X.509v3 憑證的雜湊程式碼傳回為整數。Returns the hash code for the X.509v3 certificate as an integer.

GetIssuerName()

傳回發出 X.509v3 憑證的憑證授權單位名稱。Returns the name of the certification authority that issued the X.509v3 certificate.

GetKeyAlgorithm()

傳回做為字串的這個 X.509v3 憑證金鑰演算法資訊。Returns the key algorithm information for this X.509v3 certificate as a string.

GetKeyAlgorithmParameters()

傳回做為位元組陣列的 X.509v3 憑證金鑰演算法參數。Returns the key algorithm parameters for the X.509v3 certificate as an array of bytes.

GetKeyAlgorithmParametersString()

傳回做為十六進位字串的 X.509v3 憑證金鑰演算法參數。Returns the key algorithm parameters for the X.509v3 certificate as a hexadecimal string.

GetName()

返回已核發憑證的主要名稱。Returns the name of the principal to which the certificate was issued.

GetPublicKey()

傳回做為位元組陣列的 X.509v3 憑證公開金鑰。Returns the public key for the X.509v3 certificate as an array of bytes.

GetPublicKeyString()

傳回做為十六進位字串的 X.509v3 憑證公開金鑰。Returns the public key for the X.509v3 certificate as a hexadecimal string.

GetRawCertData()

傳回做為位元組陣列的整個 X.509v3 憑證的未經處理資料。Returns the raw data for the entire X.509v3 certificate as an array of bytes.

GetRawCertDataString()

傳回做為十六進位字串的整個 X.509v3 憑證的未經處理資料。Returns the raw data for the entire X.509v3 certificate as a hexadecimal string.

GetSerialNumber()

傳回作為位元組由小到大順序之位元組陣列的 X.509v3 憑證序號。Returns the serial number of the X.509v3 certificate as an array of bytes in little-endian order.

GetSerialNumberString()

傳回作為位元組由小到大十六進位字串的 X.509v3 憑證序號。Returns the serial number of the X.509v3 certificate as a little-endian hexadecimal string .

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
Import(Byte[])

用位元組陣列的資料填入 X509Certificate 物件。Populates the X509Certificate object with data from a byte array.

Import(Byte[], SecureString, X509KeyStorageFlags)

使用位元組陣列的資料、密碼和金鑰儲存旗標,填入 X509Certificate 物件。Populates an X509Certificate object using data from a byte array, a password, and a key storage flag.

Import(Byte[], String, X509KeyStorageFlags)

用位元組陣列的資料、密碼和用於判斷如何匯入私密金鑰的旗標,填入 X509Certificate 物件。Populates the X509Certificate object using data from a byte array, a password, and flags for determining how the private key is imported.

Import(String)

用憑證檔的資訊填入 X509Certificate 物件。Populates the X509Certificate object with information from a certificate file.

Import(String, SecureString, X509KeyStorageFlags)

用憑證檔的資訊、密碼和金鑰儲存旗標,填入 X509Certificate 物件。Populates an X509Certificate object with information from a certificate file, a password, and a key storage flag.

Import(String, String, X509KeyStorageFlags)

用憑證檔案的資訊、密碼和 X509Certificate 值,填入 X509KeyStorageFlags 物件。Populates the X509Certificate object with information from a certificate file, a password, and a X509KeyStorageFlags value.

MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
Reset()

重設 X509Certificate2 物件的狀態。Resets the state of the X509Certificate2 object.

ToString()

傳回目前 X509Certificate 物件的字串表示。Returns a string representation of the current X509Certificate object.

ToString(Boolean)

傳回目前 X509Certificate 物件的字串表示,如果指定,則附帶額外資訊。Returns a string representation of the current X509Certificate object, with extra information, if specified.

TryGetCertHash(HashAlgorithmName, Span<Byte>, Int32)

明確介面實作

IDeserializationCallback.OnDeserialization(Object)

實作 ISerializable 介面並在還原序列化完成時,由還原序列化事件回呼。Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete.

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

取得重新建立目前 X509Certificate 物件的執行個體所需之全部資料的序列化資訊。Gets serialization information with all the data needed to recreate an instance of the current X509Certificate object.

適用於