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 針對標準名稱和標準位置的每個組合建立 物件。 它會使用 OpenFlags.OpenExistingOnly 旗標呼叫 Open 方法,只有當實體存放區已經存在時,才會開啟實體存放區。 如果實體存放區存在,此範例會 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.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 存放區。

重要

從 .NET Framework 4.6 開始,此類型會實作 IDisposable 介面。 當您完成使用型別時,您應該直接或間接處置它。 若要直接處置型別,請呼叫其 try/catch 區塊中的 Dispose 方法。 若要間接處置它,請使用語言建構函式,例如 using (在 C# 中) 或 Using (在 Visual Basic 中)。 如需詳細資訊,請參閱 IDisposable 介面文章中的<使用實作 IDisposable 的物件>一節。

對於以 .NET Framework 4.5.2 和更早版本為目標的應用程式,類別 X509Store 不會實 IDisposable 作 介面,因此沒有 Dispose 方法。

建構函式

X509Store()

使用目前使用者的個人憑證存放區,以初始化 X509Store 類別的新執行個體。

X509Store(IntPtr)

使用 HCERTSTORE 存放區的 Intptr 控制代碼,初始化 X509Store 類別的新執行個體。

X509Store(StoreLocation)

使用指定存放區位置值中個人憑證存放區,以初始化 X509Store 類別的新執行個體。

X509Store(StoreName)

使用目前使用者憑證存放區中指定的存放區名稱,以初始化 X509Store 類別的新執行個體。

X509Store(StoreName, StoreLocation)

使用指定的 StoreNameStoreLocation 值,初始化 X509Store 類別的新執行個體。

X509Store(StoreName, StoreLocation, OpenFlags)

使用指定的存放區名稱和存放區位置值,以初始化 X509Store 類別的新執行個體,然後使用指定的旗標開啟此執行個體。

X509Store(String)

使用指定的存放區名稱,初始化 X509Store 類別的新執行個體。

X509Store(String, StoreLocation)

使用指定的存放區名稱和存放區位置,以初始化 X509Store 類別的新執行個體。

X509Store(String, StoreLocation, OpenFlags)

使用指定的存放區名稱和存放區位置值,以初始化 X509Store 類別的新執行個體,然後使用指定的旗標開啟此執行個體。

屬性

Certificates

傳回位於 X.509 憑證存放區的憑證集合。

IsOpen

取得值,指出執行個體是否連線到憑證存放區。

Location

取得 X.509 憑證存放區的位置。

Name

取得 X.509 憑證存放區的名稱。

StoreHandle

取得 HCERTSTORE 存放區的 IntPtr 控制代碼。

方法

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)

適用於