X509Store 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表 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 物件。 它會用 flag Open 呼叫OpenFlags.OpenExistingOnly方法,只有在實體儲存庫已經存在時才會開啟。 若實體儲存庫存在,範例會使用 Name、 Location及 Certificates 屬性來顯示儲存庫中的憑證數量。
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 中)。 如需詳細資訊,請參閱介面主題中的
對於針對 .NET Framework 4.5.2 及更早版本的應用程式,該X509Store類別不實作介面IDisposable,因此沒有方法。Dispose
建構函式
| 名稱 | Description |
|---|---|
| X509Store() |
利用目前使用者的個人憑證儲存庫初始化該 X509Store 類別的新實例。 |
| X509Store(IntPtr) |
使用 Intptr 句柄初始化該類別的新實例 X509Store 到 |
| 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 實例。 |
屬性
| 名稱 | Description |
|---|---|
| Certificates |
回傳一組位於 X.509 證書庫的憑證集合。 |
| IsOpen |
會獲得一個值,表示該實例是否連接到開放的憑證儲存庫。 |
| Location |
取得 X.509 憑證儲存庫的位置。 |
| Name |
取得 X.509 憑證儲存庫的名稱。 |
| StoreHandle |
拿 IntPtr 到 |
方法
| 名稱 | Description |
|---|---|
| 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) |