语言

X509Store 类

定义

表示 X.509 存储,这是一个物理存储区,其中保存和管理证书。 此类不能被继承。

public ref class X509Store sealed : IDisposable
public ref class X509Store sealed
public sealed class X509Store : IDisposable
public sealed class X509Store
type X509Store = class
    interface IDisposable
type X509Store = class
Public NotInheritable Class X509Store
Implements IDisposable
Public NotInheritable Class X509Store
继承
X509Store
实现

示例

本部分包含两个示例。 第一个示例演示如何打开标准 X.509 存储并列出每个存储中的证书数。

第二个示例演示如何添加和删除单个证书和证书范围。

示例 1

本示例尝试在当前计算机上的每个标准位置打开每个标准存储。 它输出一个摘要,其中显示了每个存储是否存在,如果是,则显示它包含的证书数。

该示例为标准名称和标准位置的每个组合创建一个 X509Store 对象。 它使用标志调用 Open 该方法 OpenFlags.OpenExistingOnly ,该方法仅在物理存储已存在时才打开物理存储。 如果物理存储存在,则本示例使用Name和LocationCertificates属性显示存储区中的证书数。

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

public class Example
{
    static void Main()
    {
        Console.WriteLine("\r\nExists Certs Name and Location");
        Console.WriteLine("------ ----- -------------------------");

        foreach (StoreLocation storeLocation in (StoreLocation[])
            Enum.GetValues(typeof(StoreLocation)))
        {
            foreach (StoreName storeName in (StoreName[])
                Enum.GetValues(typeof(StoreName)))
            {
                X509Store store = new X509Store(storeName, storeLocation);

                try
                {
                    store.Open(OpenFlags.OpenExistingOnly);

                    Console.WriteLine("Yes    {0,4}  {1}, {2}",
                        store.Certificates.Count, store.Name, store.Location);
                }
                catch (CryptographicException)
                {
                    Console.WriteLine("No           {0}, {1}",
                        store.Name, store.Location);
                }
            }
            Console.WriteLine();
        }
    }
}

/* This example produces output similar to the following:

Exists Certs Name and Location
------ ----- -------------------------
Yes       1  AddressBook, CurrentUser
Yes      25  AuthRoot, CurrentUser
Yes     136  CA, CurrentUser
Yes      55  Disallowed, CurrentUser
Yes      20  My, CurrentUser
Yes      36  Root, CurrentUser
Yes       0  TrustedPeople, CurrentUser
Yes       1  TrustedPublisher, CurrentUser

No           AddressBook, LocalMachine
Yes      25  AuthRoot, LocalMachine
Yes     131  CA, LocalMachine
Yes      55  Disallowed, LocalMachine
Yes       3  My, LocalMachine
Yes      36  Root, LocalMachine
Yes       0  TrustedPeople, LocalMachine
Yes       1  TrustedPublisher, LocalMachine

 */
Option Strict On

Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates

Module Example

    Sub Main()

        Console.WriteLine(vbCrLf & "Exists Certs Name and Location")
        Console.WriteLine("------ ----- -------------------------")

        For Each storeLocation As StoreLocation In _
                CType([Enum].GetValues(GetType(StoreLocation)), StoreLocation())

            For Each storeName As StoreName In _
                    CType([Enum].GetValues(GetType(StoreName)), StoreName())

                Dim store As New X509Store(StoreName, StoreLocation)

                Try
                    store.Open(OpenFlags.OpenExistingOnly)
                    Console.WriteLine("Yes    {0,4}  {1}, {2}", _
                        store.Certificates.Count, store.Name, store.Location)
                Catch e As CryptographicException
                    Console.WriteLine("No           {0}, {1}", _
                        store.Name, store.Location)
                End Try
            Next

            Console.WriteLine()
        Next
    End Sub
End Module

' This example produces output similar to the following:

'Exists Certs Name and Location
'------ ----- -------------------------
'Yes       1  AddressBook, CurrentUser
'Yes      25  AuthRoot, CurrentUser
'Yes     136  CA, CurrentUser
'Yes      55  Disallowed, CurrentUser
'Yes      20  My, CurrentUser
'Yes      36  Root, CurrentUser
'Yes       0  TrustedPeople, CurrentUser
'Yes       1  TrustedPublisher, CurrentUser

'No           AddressBook, LocalMachine
'Yes      25  AuthRoot, LocalMachine
'Yes     131  CA, LocalMachine
'Yes      55  Disallowed, LocalMachine
'Yes       3  My, LocalMachine
'Yes      36  Root, LocalMachine
'Yes       0  TrustedPeople, LocalMachine
'Yes       1  TrustedPublisher, LocalMachine

示例 2

本示例打开 X.509 证书存储区,添加和删除证书,然后关闭存储区。 它假定你有三个证书,可在本地存储中添加和删除。

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

public class X509store2
{
    public static void Main (string[] args)
    {
        //Create new X509 store called teststore from the local certificate store.
        X509Store store = new X509Store ("teststore", StoreLocation.CurrentUser);
        store.Open (OpenFlags.ReadWrite);
        X509Certificate2 certificate = new X509Certificate2 ();

        //Create certificates from certificate files.
        //You must put in a valid path to three certificates in the following constructors.
        X509Certificate2 certificate1 = new X509Certificate2 ("c:\\mycerts\\*****.cer");
        X509Certificate2 certificate2 = new X509Certificate2 ("c:\\mycerts\\*****.cer");
        X509Certificate2 certificate5 = new X509Certificate2 ("c:\\mycerts\\*****.cer");

        //Create a collection and add two of the certificates.
        X509Certificate2Collection collection = new X509Certificate2Collection ();
        collection.Add (certificate2);
        collection.Add (certificate5);

        //Add certificates to the store.
        store.Add (certificate1);
        store.AddRange (collection);

        X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
        Console.WriteLine ("Store name: {0}", store.Name);
        Console.WriteLine ("Store location: {0}", store.Location);
        foreach (X509Certificate2 x509 in storecollection)
        {
            Console.WriteLine("certificate name: {0}",x509.Subject);
        }

        //Remove a certificate.
        store.Remove (certificate1);
        X509Certificate2Collection storecollection2 = (X509Certificate2Collection)store.Certificates;
        Console.WriteLine ("{1}Store name: {0}", store.Name, Environment.NewLine);
        foreach (X509Certificate2 x509 in storecollection2)
        {
            Console.WriteLine ("certificate name: {0}", x509.Subject);
        }

        //Remove a range of certificates.
        store.RemoveRange (collection);
        X509Certificate2Collection storecollection3 = (X509Certificate2Collection)store.Certificates;
        Console.WriteLine ("{1}Store name: {0}", store.Name, Environment.NewLine);
        if (storecollection3.Count == 0)
        {
            Console.WriteLine ("Store contains no certificates.");
        }
        else
        {
            foreach (X509Certificate2 x509 in storecollection3)
            {
                Console.WriteLine ("certificate name: {0}", x509.Subject);
            }
        }

        //Close the store.
        store.Close ();
    }	
}
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.IO



Class X509store2

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

        'Create certificates from certificate files.
        'You must put in a valid path to three certificates in the following constructors.
        Dim certificate1 As New X509Certificate2("c:\mycerts\*****.cer")
        Dim certificate2 As New X509Certificate2("c:\mycerts\*****.cer")
        Dim certificate5 As New X509Certificate2("c:\mycerts\*****.cer")

        'Create a collection and add two of the certificates.
        Dim collection As New X509Certificate2Collection()
        collection.Add(certificate2)
        collection.Add(certificate5)

        'Add certificates to the store.
        store.Add(certificate1)
        store.AddRange(collection)

        Dim storecollection As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        Console.WriteLine("Store name: {0}", store.Name)
        Console.WriteLine("Store location: {0}", store.Location)
        Dim x509 As X509Certificate2
        For Each x509 In storecollection
            Console.WriteLine("certificate name: {0}", x509.Subject)
        Next x509

        'Remove a certificate.
        store.Remove(certificate1)
        Dim storecollection2 As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        Console.WriteLine("{1}Store name: {0}", store.Name, Environment.NewLine)
        Dim x509a As X509Certificate2
        For Each x509a In storecollection2
            Console.WriteLine("certificate name: {0}", x509a.Subject)
        Next x509a

        'Remove a range of certificates.
        store.RemoveRange(collection)
        Dim storecollection3 As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        Console.WriteLine("{1}Store name: {0}", store.Name, Environment.NewLine)
        If storecollection3.Count = 0 Then
            Console.WriteLine("Store contains no certificates.")
        Else
            Dim x509b As X509Certificate2
            For Each x509b In storecollection3
                Console.WriteLine("certificate name: {0}", x509b.Subject)
            Next x509b
        End If

        'Close the store.
        store.Close()

    End Sub
End Class

注解

使用此类处理 X.509 存储。

Important

从 .NET Framework 4.6 开始,此类型实现 IDisposable 接口。 使用完该类型后,应直接或间接处理它。 若要直接释放类型,请在块中Disposetry/调用其catch方法。 若要间接释放它,请使用语言构造,例如 using (在 C# 中)或 Using (在 Visual Basic 中)。 有关详细信息,请参阅接口主题中的 IDisposable “使用实现 IDisposable 的对象”部分。

对于面向 .NET Framework 4.5.2 及更低版本的应用,该 X509Store 类不实现 IDisposable 接口,因此没有方法 Dispose 。

构造函数

名称 说明
X509Store()

使用当前用户的个人证书存储初始化类的新实例 X509Store 。

X509Store(IntPtr)

使用 Intptr 句柄初始化X509Store存储区的新类实例HCERTSTORE。

X509Store(StoreLocation)

使用指定存储位置值中的个人证书存储初始化类的新实例 X509Store 。

X509Store(StoreName, StoreLocation, OpenFlags)

使用指定的存储名称和存储位置值初始化类的新实例 X509Store ,然后使用指定的标志打开它。

X509Store(StoreName, StoreLocation)

使用指定的X509Store值初始化StoreName类的新实例StoreLocation。

X509Store(StoreName)

使用当前用户的证书存储中的指定存储名称初始化类的新实例 X509Store 。

X509Store(String, StoreLocation, OpenFlags)

使用指定的存储名称和存储位置值初始化类的新实例 X509Store ,然后使用指定的标志打开它。

X509Store(String, StoreLocation)

使用指定的存储名称和存储位置初始化类的新实例 X509Store 。

X509Store(String)

使用指定的存储名称初始化类的新实例 X509Store 。

属性

名称 说明
Certificates

返回位于 X.509 证书存储区中的证书集合。

IsOpen

获取一个值,该值指示实例是否连接到打开的证书存储。

Location

获取 X.509 证书存储区的位置。

Name

获取 X.509 证书存储区的名称。

StoreHandle

IntPtr获取存储的HCERTSTORE句柄。

方法

名称 说明
Add(X509Certificate2)

将证书添加到 X.509 证书存储区。

AddRange(X509Certificate2Collection)

将证书集合添加到 X.509 证书存储区。

Close()

关闭 X.509 证书存储。

Dispose()

释放此 X509Store资源。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type。

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Open(OpenFlags)

打开 X.509 证书存储或创建新存储,具体取决于 OpenFlags 标志设置。

Remove(X509Certificate2)

从 X.509 证书存储中删除证书。

RemoveRange(X509Certificate2Collection)

从 X.509 证书存储中删除一系列证书。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

适用于