X509Chain X509Chain X509Chain X509Chain Class


表示 X509Certificate2 憑證的鏈結建置引擎。Represents a chain-building engine for X509Certificate2 certificates.

public ref class X509Chain : IDisposable
public class X509Chain : IDisposable
type X509Chain = class
    interface IDisposable
Public Class X509Chain
Implements IDisposable


下列程式碼範例會開啟目前使用者的個人憑證存放區,可讓您選取憑證,然後寫入主控台中的憑證和憑證鏈結的資訊。The following code example opens the current user's personal certificate store, allows you to select a certificate, then writes certificate and certificate chain information to the console. 輸出取決於您選取的憑證。The output depends on the certificate you select.

#using <System.dll>
#using <System.Security.dll>

using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Security::Cryptography::X509Certificates;
using namespace System::IO;

int main()
   //Create new X509 store from local certificate store.
   X509Store ^ store = gcnew X509Store( "MY",StoreLocation::CurrentUser );
   store->Open( static_cast<OpenFlags>(OpenFlags::OpenExistingOnly | OpenFlags::ReadWrite) );

   //Output store information.
   Console::WriteLine( "Store Information" );
   Console::WriteLine( "Number of certificates in the store: {0}", store->Certificates->Count );
   Console::WriteLine( "Store location: {0}", store->Location );
   Console::WriteLine( "Store name: {0} {1}", store->Name, Environment::NewLine );

   //Put certificates from the store into a collection so user can select one.
   X509Certificate2Collection ^ fcollection = dynamic_cast<X509Certificate2Collection^>(store->Certificates);
   X509Certificate2Collection ^ collection = X509Certificate2UI::SelectFromCollection(fcollection, "Select an X509 Certificate","Choose a certificate to examine.",X509SelectionFlag::SingleSelection);
   X509Certificate2 ^ certificate = collection[ 0 ];

   //Output chain information of the selected certificate.
   X509Chain ^ ch = gcnew X509Chain;
   ch->ChainPolicy->RevocationMode = X509RevocationMode::Online;
   ch->Build( certificate );
   Console::WriteLine( "Chain Information" );
   Console::WriteLine( "Chain revocation flag: {0}", ch->ChainPolicy->RevocationFlag );
   Console::WriteLine( "Chain revocation mode: {0}", ch->ChainPolicy->RevocationMode );
   Console::WriteLine( "Chain verification flag: {0}", ch->ChainPolicy->VerificationFlags );
   Console::WriteLine( "Chain verification time: {0}", ch->ChainPolicy->VerificationTime );
   Console::WriteLine( "Chain status length: {0}", ch->ChainStatus->Length );
   Console::WriteLine( "Chain application policy count: {0}", ch->ChainPolicy->ApplicationPolicy->Count );
   Console::WriteLine( "Chain certificate policy count: {0} {1}", ch->ChainPolicy->CertificatePolicy->Count, Environment::NewLine );

   //Output chain element information.
   Console::WriteLine( "Chain Element Information" );
   Console::WriteLine( "Number of chain elements: {0}", ch->ChainElements->Count );
   Console::WriteLine( "Chain elements synchronized? {0} {1}", ch->ChainElements->IsSynchronized, Environment::NewLine );
   System::Collections::IEnumerator^ myEnum = ch->ChainElements->GetEnumerator();
   while ( myEnum->MoveNext() )
      X509ChainElement ^ element = safe_cast<X509ChainElement ^>(myEnum->Current);
      Console::WriteLine( "Element issuer name: {0}", element->Certificate->Issuer );
      Console::WriteLine( "Element certificate valid until: {0}", element->Certificate->NotAfter );
      Console::WriteLine( "Element certificate is valid: {0}", element->Certificate->Verify() );
      Console::WriteLine( "Element error status length: {0}", element->ChainElementStatus->Length );
      Console::WriteLine( "Element information: {0}", element->Information );
      Console::WriteLine( "Number of element extensions: {0}{1}", element->Certificate->Extensions->Count, Environment::NewLine );
      if ( ch->ChainStatus->Length > 1 )
         for ( int index = 0; index < element->ChainElementStatus->Length; index++ )
            Console::WriteLine( element->ChainElementStatus[ index ].Status );
            Console::WriteLine( element->ChainElementStatus[ index ].StatusInformation );

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.IO;

class TestX509Chain
	static void Main(string[] args)
		//Create new X509 store from local certificate store.
		X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
		store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);

		//Output store information.
		Console.WriteLine ("Store Information");
		Console.WriteLine ("Number of certificates in the store: {0}", store.Certificates.Count);
		Console.WriteLine ("Store location: {0}", store.Location);
		Console.WriteLine ("Store name: {0} {1}", store.Name, Environment.NewLine);
		//Put certificates from the store into a collection so user can select one.
		X509Certificate2Collection fcollection = (X509Certificate2Collection)store.Certificates;
		X509Certificate2Collection collection = X509Certificate2UI.SelectFromCollection(fcollection, "Select an X509 Certificate", "Choose a certificate to examine.", X509SelectionFlag.SingleSelection);
		X509Certificate2 certificate = collection[0];

		//Output chain information of the selected certificate.
		X509Chain ch = new X509Chain();
		ch.ChainPolicy.RevocationMode = X509RevocationMode.Online;
		ch.Build (certificate);
		Console.WriteLine ("Chain Information");
		Console.WriteLine ("Chain revocation flag: {0}", ch.ChainPolicy.RevocationFlag);
		Console.WriteLine ("Chain revocation mode: {0}", ch.ChainPolicy.RevocationMode);
		Console.WriteLine ("Chain verification flag: {0}", ch.ChainPolicy.VerificationFlags);
		Console.WriteLine ("Chain verification time: {0}", ch.ChainPolicy.VerificationTime);
		Console.WriteLine ("Chain status length: {0}", ch.ChainStatus.Length);
		Console.WriteLine ("Chain application policy count: {0}", ch.ChainPolicy.ApplicationPolicy.Count);
		Console.WriteLine ("Chain certificate policy count: {0} {1}", ch.ChainPolicy.CertificatePolicy.Count, Environment.NewLine);

		//Output chain element information.
		Console.WriteLine ("Chain Element Information");
		Console.WriteLine ("Number of chain elements: {0}", ch.ChainElements.Count);
		Console.WriteLine ("Chain elements synchronized? {0} {1}", ch.ChainElements.IsSynchronized, Environment.NewLine);
		foreach (X509ChainElement element in ch.ChainElements)
			Console.WriteLine ("Element issuer name: {0}", element.Certificate.Issuer);
			Console.WriteLine ("Element certificate valid until: {0}", element.Certificate.NotAfter);
			Console.WriteLine ("Element certificate is valid: {0}", element.Certificate.Verify ());
			Console.WriteLine ("Element error status length: {0}", element.ChainElementStatus.Length);
			Console.WriteLine ("Element information: {0}", element.Information);
			Console.WriteLine ("Number of element extensions: {0}{1}", element.Certificate.Extensions.Count, Environment.NewLine);

			if (ch.ChainStatus.Length > 1)
				for (int index = 0; index < element.ChainElementStatus.Length; index++)
					Console.WriteLine (element.ChainElementStatus[index].Status);
					Console.WriteLine (element.ChainElementStatus[index].StatusInformation);
Imports System
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.IO

Class TestX509Chain

    Shared Sub Main(ByVal args() As String)
        'Create new X509 store from local certificate store.
        Dim store As New X509Store("MY", StoreLocation.CurrentUser)
        store.Open(OpenFlags.OpenExistingOnly Or OpenFlags.ReadWrite)

        'Output store information.
        Console.WriteLine("Store Information")
        Console.WriteLine("Number of certificates in the store: {0}", store.Certificates.Count)
        Console.WriteLine("Store location: {0}", store.Location)
        Console.WriteLine("Store name: {0} {1}", store.Name, Environment.NewLine)

        'Put certificates from the store into a collection so user can select one.
        Dim fcollection As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        Dim collection As X509Certificate2Collection = X509Certificate2UI.SelectFromCollection(fcollection, "Select an X509 Certificate", "Choose a certificate to examine.", X509SelectionFlag.SingleSelection)
        Dim certificate As X509Certificate2 = collection(0)

        'Output chain information of the selected certificate.
        Dim ch As New X509Chain()
        ch.ChainPolicy.RevocationMode = X509RevocationMode.Online
        Console.WriteLine("Chain Information")
        Console.WriteLine("Chain revocation flag: {0}", ch.ChainPolicy.RevocationFlag)
        Console.WriteLine("Chain revocation mode: {0}", ch.ChainPolicy.RevocationMode)
        Console.WriteLine("Chain verification flag: {0}", ch.ChainPolicy.VerificationFlags)
        Console.WriteLine("Chain verification time: {0}", ch.ChainPolicy.VerificationTime)
        Console.WriteLine("Chain status length: {0}", ch.ChainStatus.Length)
        Console.WriteLine("Chain application policy count: {0}", ch.ChainPolicy.ApplicationPolicy.Count)
        Console.WriteLine("Chain certificate policy count: {0} {1}", ch.ChainPolicy.CertificatePolicy.Count, Environment.NewLine)

        'Output chain element information.
        Console.WriteLine("Chain Element Information")
        Console.WriteLine("Number of chain elements: {0}", ch.ChainElements.Count)
        Console.WriteLine("Chain elements synchronized? {0} {1}", ch.ChainElements.IsSynchronized, Environment.NewLine)

        Dim element As X509ChainElement
        For Each element In ch.ChainElements
            Console.WriteLine("Element issuer name: {0}", element.Certificate.Issuer)
            Console.WriteLine("Element certificate valid until: {0}", element.Certificate.NotAfter)
            Console.WriteLine("Element certificate is valid: {0}", element.Certificate.Verify())
            Console.WriteLine("Element error status length: {0}", element.ChainElementStatus.Length)
            Console.WriteLine("Element information: {0}", element.Information)
            Console.WriteLine("Number of element extensions: {0}{1}", element.Certificate.Extensions.Count, Environment.NewLine)

            If ch.ChainStatus.Length > 1 Then
                Dim index As Integer
                For index = 0 To element.ChainElementStatus.Length
                Next index
            End If
        Next element
    End Sub
End Class


X509Chain物件具有全域錯誤狀態,稱為ChainStatus,應該用於憑證驗證。The X509Chain object has a global error status called ChainStatus that should be used for certificate validation. 管理憑證驗證的規則很複雜,並很容易忽略一或多個項目相關的錯誤狀態時,過度的驗證邏輯。The rules governing certificate validation are complex, and it is easy to oversimplify the validation logic by ignoring the error status of one or more of the elements involved. 全域錯誤狀態的狀態鏈結中每個項目納入考量。The global error status takes into consideration the status of each element in the chain.


開頭.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. 若要直接處置型別,呼叫其Dispose方法中的try / catch區塊。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和更早的版本X509Chain類別不會實作IDisposable介面,並因此不需要Dispose方法。For apps that target the .NET Framework 4.5.2.NET Framework 4.5.2 and earlier versions, the X509Chain class does not implement the IDisposable interface and therefore does not have a Dispose method.


X509Chain() X509Chain() X509Chain() X509Chain()

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

X509Chain(Boolean) X509Chain(Boolean) X509Chain(Boolean) X509Chain(Boolean)

初始化 X509Chain 類別的新執行個體,並指定一個值,指出是否應該使用電腦內容。Initializes a new instance of the X509Chain class specifying a value that indicates whether the machine context should be used.

X509Chain(IntPtr) X509Chain(IntPtr) X509Chain(IntPtr) X509Chain(IntPtr)

使用 X.509 鏈結的 X509Chain 控制代碼,初始化 IntPtr 類別的新執行個體。Initializes a new instance of the X509Chain class using an IntPtr handle to an X.509 chain.


ChainContext ChainContext ChainContext ChainContext

取得 X.509 鏈結的控制代碼。Gets a handle to an X.509 chain.

ChainElements ChainElements ChainElements ChainElements

取得 X509ChainElement 物件的集合。Gets a collection of X509ChainElement objects.

ChainPolicy ChainPolicy ChainPolicy ChainPolicy

取得或設定 X509ChainPolicy,以在建置 X.509 憑證鏈結時使用。Gets or sets the X509ChainPolicy to use when building an X.509 certificate chain.

ChainStatus ChainStatus ChainStatus ChainStatus

取得 X509Chain 物件中每個項目的狀態。Gets the status of each element in an X509Chain object.

SafeHandle SafeHandle SafeHandle SafeHandle

取得此 X509Chain 執行個體的安全控制代碼。Gets a safe handle for this X509Chain instance.


Build(X509Certificate2) Build(X509Certificate2) Build(X509Certificate2) Build(X509Certificate2)

使用 X509ChainPolicy 中指定的原則,建置 X.509 鏈結。Builds an X.509 chain using the policy specified in X509ChainPolicy.

Create() Create() Create() Create()

在查詢 CryptoConfig 檔案中定義的對應之後,建立 X509Chain 物件,並將鏈結對應至該對應。Creates an X509Chain object after querying for the mapping defined in the CryptoConfig file, and maps the chain to that mapping.

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

釋放這個 X509Chain 使用的所有資源。Releases all of the resources used by this X509Chain.

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

釋放這個 X509Chain 所使用的非受控資源,並選擇性地釋放受控資源。Releases the unmanaged resources used by this X509Chain, and optionally releases the managed resources.

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

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize() Finalize() Finalize() Finalize()
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

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

清除目前的 X509Chain 物件。Clears the current X509Chain object.

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

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)