X509Store X509Store X509Store X509Store Class

定义

表示 X.509 存储区,该存储区是保留和管理证书的物理存储区。Represents an X.509 store, which is a physical store where certificates are persisted and managed. 此类不能被继承。This class cannot be inherited.

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

示例

本部分包含两个示例。This section contains two examples. 第一个示例演示如何打开标准 x.509 存储, 并列出每个存储中的证书数量。The first example demonstrates how you can open standard X.509 stores and list the number of certificates in each.

第二个示例演示如何添加和删除单个证书和证书范围。The second example demonstrates how you can add and remove single certificates and ranges of certificates.

示例1Example 1

此示例尝试打开当前计算机上每个标准位置中的每个标准存储区。This example tries to open each standard store in each standard location on the current computer. 它将打印摘要, 其中显示每个存储是否存在, 如果存在, 则显示它包含的证书的数量。It prints a summary that shows whether each store exists and, if so, the number of certificates it contains.

该示例为标准X509Store名称和标准位置的每个组合创建一个对象。The example creates an X509Store object for each combination of standard name and standard location. OpenOpenFlags.OpenExistingOnly通过标志调用方法, 该标志仅在已存在时打开物理存储区。It calls the Open method with the OpenFlags.OpenExistingOnly flag, which opens the physical store only if it already exists. 如果物理存储存在, 则该示例使用NameLocationCertificates属性显示存储区中的证书数量。If the physical store exists, the example uses the Name, Location, and Certificates properties to display the number of certificates in the store.

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

示例2Example 2

此示例将打开一个 x.509 证书存储区, 添加并删除证书, 然后关闭存储区。This example opens an X.509 certificate store, adds and deletes certificates, and then closes the store. 它假定你有三个要在本地存储区中添加和删除的证书。It assumes that you have three certificates to add to and remove from a local store.

#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 called teststore from the local certificate store.
   X509Store ^ store = gcnew X509Store( "teststore",StoreLocation::CurrentUser );
   store->Open( OpenFlags::ReadWrite );
   X509Certificate2 ^ certificate = gcnew X509Certificate2;
   
   //Create certificates from certificate files.
   //You must put in a valid path to three certificates in the following constructors.
   X509Certificate2 ^ certificate1 = gcnew X509Certificate2( "c:\\mycerts\\*****.cer" );
   X509Certificate2 ^ certificate2 = gcnew X509Certificate2( "c:\\mycerts\\*****.cer" );
   X509Certificate2 ^ certificate5 = gcnew X509Certificate2( "c:\\mycerts\\*****.cer" );
   
   //Create a collection and add two of the certificates.
   X509Certificate2Collection ^ collection = gcnew X509Certificate2Collection;
   collection->Add( certificate2 );
   collection->Add( certificate5 );
   
   //Add certificates to the store.
   store->Add( certificate1 );
   store->AddRange( collection );
   X509Certificate2Collection ^ storecollection = dynamic_cast<X509Certificate2Collection^>(store->Certificates);
   Console::WriteLine( "Store name: {0}", store->Name );
   Console::WriteLine( "Store location: {0}", store->Location );
   System::Collections::IEnumerator^ myEnum = storecollection->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      X509Certificate2 ^ x509 = safe_cast<X509Certificate2 ^>(myEnum->Current);
      Console::WriteLine( "certificate name: {0}", x509->Subject );
   }

   
   //Remove a certificate.
   store->Remove( certificate1 );
   X509Certificate2Collection ^ storecollection2 = dynamic_cast<X509Certificate2Collection^>(store->Certificates);
   Console::WriteLine( "{1}Store name: {0}", store->Name, Environment::NewLine );
   System::Collections::IEnumerator^ myEnum1 = storecollection2->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      X509Certificate2 ^ x509 = safe_cast<X509Certificate2 ^>(myEnum1->Current);
      Console::WriteLine( "certificate name: {0}", x509->Subject );
   }

   
   //Remove a range of certificates.
   store->RemoveRange( collection );
   X509Certificate2Collection ^ storecollection3 = dynamic_cast<X509Certificate2Collection^>(store->Certificates);
   Console::WriteLine( "{1}Store name: {0}", store->Name, Environment::NewLine );
   if ( storecollection3->Count == 0 )
   {
      Console::WriteLine( "Store contains no certificates." );
   }
   else
   {
      System::Collections::IEnumerator^ myEnum2 = storecollection3->GetEnumerator();
      while ( myEnum2->MoveNext() )
      {
         X509Certificate2 ^ x509 = safe_cast<X509Certificate2 ^>(myEnum2->Current);
         Console::WriteLine( "certificate name: {0}", x509->Subject );
      }
   }

   
   //Close the store.
   store->Close();
}

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 存储。Use this class to work with an X.509 store.

重要

从开始IDisposable , 此类型实现接口。 .NET Framework 4.6.NET Framework 4.6Starting 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和更早版本的应用X509Store , IDisposable类不实现接口, 因此不具有Dispose方法。For apps that target the .NET Framework 4.5.2.NET Framework 4.5.2 and earlier versions, the X509Store class does not implement the IDisposable interface and therefore does not have a Dispose method.

构造函数

X509Store() X509Store() X509Store() X509Store()

使用当前用户存储区的个人证书初始化 X509Store 类的新实例。Initializes a new instance of the X509Store class using the personal certificates of the current user store.

X509Store(IntPtr) X509Store(IntPtr) X509Store(IntPtr) X509Store(IntPtr)

使用 HCERTSTORE 存储区的 Intptr 句柄初始化 X509Store 类的新实例。Initializes a new instance of the X509Store class using an Intptr handle to an HCERTSTORE store.

X509Store(StoreLocation) X509Store(StoreLocation) X509Store(StoreLocation) X509Store(StoreLocation)

使用指定的 StoreLocation 值初始化 X509Store 类的新实例。Initializes a new instance of the X509Store class using the specified StoreLocation value.

X509Store(StoreName) X509Store(StoreName) X509Store(StoreName) X509Store(StoreName)

使用指定的 StoreName 值初始化 X509Store 类的新实例。Initializes a new instance of the X509Store class using the specified StoreName value.

X509Store(StoreName, StoreLocation) X509Store(StoreName, StoreLocation) X509Store(StoreName, StoreLocation) X509Store(StoreName, StoreLocation)

使用指定的 X509StoreStoreName 值初始化 StoreLocation 类的新实例。Initializes a new instance of the X509Store class using the specified StoreName and StoreLocation values.

X509Store(StoreName, StoreLocation, OpenFlags) X509Store(StoreName, StoreLocation, OpenFlags) X509Store(StoreName, StoreLocation, OpenFlags) X509Store(StoreName, StoreLocation, OpenFlags)
X509Store(String) X509Store(String) X509Store(String) X509Store(String)

使用指定的存储区名称初始化 X509Store 类的新实例。Initializes a new instance of the X509Store class using the specified store name.

X509Store(String, StoreLocation) X509Store(String, StoreLocation) X509Store(String, StoreLocation) X509Store(String, StoreLocation)

使用一个字符串初始化 X509Store 类的新实例,该字符串表示 StoreName 枚举中的一个值和 StoreLocation 枚举中的一个值。Initializes a new instance of the X509Store class using a string that represents a value from the StoreName enumeration and a value from the StoreLocation enumeration.

X509Store(String, StoreLocation, OpenFlags) X509Store(String, StoreLocation, OpenFlags) X509Store(String, StoreLocation, OpenFlags) X509Store(String, StoreLocation, OpenFlags)

属性

Certificates Certificates Certificates Certificates

返回位于 X.509 证书存储区中的证书集合。Returns a collection of certificates located in an X.509 certificate store.

IsOpen IsOpen IsOpen IsOpen
Location Location Location Location

获取 X.509 证书存储区的位置。Gets the location of the X.509 certificate store.

Name Name Name Name

获取 X.509 证书存储区的名称。Gets the name of the X.509 certificate store.

StoreHandle StoreHandle StoreHandle StoreHandle

获取 HCERTSTORE 存储区的 IntPtr 句柄。Gets an IntPtr handle to an HCERTSTORE store.

方法

Add(X509Certificate2) Add(X509Certificate2) Add(X509Certificate2) Add(X509Certificate2)

将证书添加到 X.509 证书存储区。Adds a certificate to an X.509 certificate store.

AddRange(X509Certificate2Collection) AddRange(X509Certificate2Collection) AddRange(X509Certificate2Collection) AddRange(X509Certificate2Collection)

将证书集合添加到 X.509 证书存储。Adds a collection of certificates to an X.509 certificate store.

Close() Close() Close() Close()

关闭 X.509 证书存储区。Closes an X.509 certificate store.

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

释放由此 X509Store 使用的资源。Releases the resources used by this X509Store.

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

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
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 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Open(OpenFlags) Open(OpenFlags) Open(OpenFlags) Open(OpenFlags)

打开 X.509 证书存储区或创建新存储区,具体取决于 OpenFlags 标志设置。Opens an X.509 certificate store or creates a new store, depending on OpenFlags flag settings.

Remove(X509Certificate2) Remove(X509Certificate2) Remove(X509Certificate2) Remove(X509Certificate2)

从 X.509 证书存储区移除证书。Removes a certificate from an X.509 certificate store.

RemoveRange(X509Certificate2Collection) RemoveRange(X509Certificate2Collection) RemoveRange(X509Certificate2Collection) RemoveRange(X509Certificate2Collection)

从 X.509 证书存储区移除一系列证书。Removes a range of certificates from an X.509 certificate store.

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

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于