X509Chain 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表證書鏈條建立引擎 X509Certificate2 。
public ref class X509Chain : IDisposable
public ref class X509Chain
public class X509Chain : IDisposable
public class X509Chain
type X509Chain = class
interface IDisposable
type X509Chain = class
Public Class X509Chain
Implements IDisposable
Public Class X509Chain
- 繼承
-
X509Chain
- 實作
範例
以下程式碼範例會開啟目前使用者的個人憑證儲存庫,允許你選擇憑證,然後將憑證與憑證鏈資訊寫入主控台。 產出取決於你選擇的證書。
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];
X509Certificate2UI.DisplayCertificate(certificate);
//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);
}
}
}
store.Close();
}
}
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)
X509Certificate2UI.DisplayCertificate(certificate)
'Output chain information of the selected certificate.
Dim ch As 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)
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
Console.WriteLine(element.ChainElementStatus(index).Status)
Console.WriteLine(element.ChainElementStatus(index).StatusInformation)
Next index
End If
Next element
store.Close()
End Sub
End Class
備註
該 X509Chain 物件有一個全域錯誤狀態,應該 ChainStatus 用於憑證驗證。 憑證驗證的規則相當複雜,若忽略一個或多個相關元素的錯誤狀態,很容易簡化驗證邏輯。 全域錯誤狀態會考慮鏈中每個元素的狀態。
Important
從 .NET Framework 4.6 開始,這種介面類型實現了。IDisposable 當您完成使用這個物品後,應直接或間接地處理它。 若要直接處置類型,請在 Disposetry/ 區塊中呼叫其 catch 方法。 若要間接處置它,請使用語言建構,例如 using (C#) 或 Using (在 Visual Basic 中)。 如需詳細資訊,請參閱介面主題中的
對於針對 .NET Framework 4.5.2 及更早版本的應用程式,該X509Chain類別不實作介面IDisposable,因此沒有方法。Dispose
在處理 X509Chain時,憑證 ChainElements 並未被處理。 當憑證實例不再需要時,您應該處理本集合中的憑證。
建構函式
| 名稱 | Description |
|---|---|
| X509Chain() |
初始化 X509Chain 類別的新執行個體。 |
| X509Chain(Boolean) |
初始化一個新的類別實例 X509Chain ,指定一個值,指示是否應使用機器上下文。 |
| X509Chain(IntPtr) |
屬性
| 名稱 | Description |
|---|---|
| ChainContext |
可以把手接到X.509的鏈條上。 |
| ChainElements |
會得到一組 X509ChainElement 物品。 |
| ChainPolicy |
在建立 X.509 憑證鏈時,取得或設定 X509ChainPolicy 要使用的 。 |
| ChainStatus |
取得物件中 X509Chain 每個元素的狀態。 |
| SafeHandle |
這次會用一個安全的控制 X509Chain 方式。 |
方法
| 名稱 | Description |
|---|---|
| Build(X509Certificate2) |
依據 X509ChainPolicy中指定的策略建構 X.509 鏈。 |
| Create() |
在查詢 CryptoConfig 檔案中定義的映射後建立 X509Chain 一個物件,並將鏈映射到該映射。 |
| Dispose() |
釋放所有用於此 X509Chain的資源。 |
| Dispose(Boolean) |
釋放此 X509Chain所使用的非管理資源,並可選擇性地釋放受管理資源。 |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| Reset() |
清除當前 X509Chain 物件。 |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |