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.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 ];
X509Certificate2UI::DisplayCertificate(certificate);
//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 );
}
}
}
store->Close();
}
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 全局错误状态,该状态应用于证书验证。 管理证书验证的规则很复杂,通过忽略涉及的一个或多个元素的错误状态,很容易使验证逻辑过于简单。 全局错误状态考虑链中每个元素的状态。
重要
从 .NET Framework 4.6 开始,此类型实现 IDisposable 接口。 在使用完类型后,您应直接或间接释放类型。 若要直接释放类型,请在 try
/catch
块中调用其 Dispose 方法。 若要间接释放类型,请使用 using
(在 C# 中)或 Using
(在 Visual Basic 中)等语言构造。 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。
对于面向 .NET Framework 4.5.2 及更早版本的应用, X509Chain 类不实现 IDisposable 接口,因此没有 Dispose
方法。
构造函数
X509Chain() |
初始化 X509Chain 类的新实例。 |
X509Chain(Boolean) |
通过指定一个值指示是否应使用计算机上下文,初始化 X509Chain 类的新实例。 |
X509Chain(IntPtr) |
属性
ChainContext |
获取 X.509 链的句柄。 |
ChainElements |
获取 X509ChainElement 对象的集合。 |
ChainPolicy |
获取或设置生成 X.509 证书链时要使用的 X509ChainPolicy。 |
ChainStatus |
获取 X509Chain 对象中每个元素的状态。 |
SafeHandle |
获取此 X509Chain 实例的安全句柄。 |
方法
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) |