X509Store 類別

定義

表示 X.509 存放區,這個存放區是保存和管理憑證的實體存放區。 此類別無法獲得繼承。

C#
public sealed class X509Store : IDisposable
C#
public sealed class X509Store
繼承
X509Store
實作

範例

本節包含兩個範例。 第一個範例示範如何開啟標準 X.509 存放區,並列出每個存放區中的憑證數目。

第二個範例示範如何新增和移除單一憑證和憑證範圍。

範例 1

本範例會嘗試在目前電腦上的每個標準位置開啟每個標準存放區。 它會列印摘要,以顯示每個存放區是否存在,如果是的話,則會列印其包含的憑證數目。

此範例會 X509Store 針對標準名稱和標準位置的每個組合建立 物件。 它會使用 OpenFlags.OpenExistingOnly 旗標呼叫 Open 方法,只有當實體存放區已經存在時,才會開啟實體存放區。 如果實體存放區存在,此範例會 Name使用、 LocationCertificates 屬性來顯示存放區中的憑證數目。

C#
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

 */

範例 2

本範例會開啟 X.509 證書存儲、新增和刪除憑證,然後關閉存放區。 它假設您有三個憑證可新增至本機存放區並從本機存放區中移除。

C#
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 ();
    }	
}

備註

使用此類別來處理 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)

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1