X509Store Klass

Definition

Representerar ett X.509-arkiv, som är ett fysiskt arkiv där certifikat sparas och hanteras. Det går inte att ärva den här klassen.

public ref class X509Store sealed
public ref class X509Store sealed : IDisposable
public sealed class X509Store
public sealed class X509Store : IDisposable
type X509Store = class
type X509Store = class
    interface IDisposable
Public NotInheritable Class X509Store
Public NotInheritable Class X509Store
Implements IDisposable
Arv
X509Store
Implementeringar

Exempel

Det här avsnittet innehåller två exempel. Det första exemplet visar hur du kan öppna X.509-standardarkiv och visa en lista över antalet certifikat i varje.

Det andra exemplet visar hur du kan lägga till och ta bort enskilda certifikat och certifikatintervall.

Exempel 1

Det här exemplet försöker öppna varje standardarkiv på varje standardplats på den aktuella datorn. Den skriver ut en sammanfattning som visar om varje arkiv finns och i så fall antalet certifikat som det innehåller.

Exemplet skapar ett X509Store objekt för varje kombination av standardnamn och standardplats. Den anropar Open metoden med OpenFlags.OpenExistingOnly flaggan, som endast öppnar det fysiska arkivet om den redan finns. Om det fysiska arkivet finns använder Nameexemplet egenskaperna , Locationoch Certificates för att visa antalet certifikat i arkivet.

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

Exempel 2

Det här exemplet öppnar ett X.509-certifikatarkiv, lägger till och tar bort certifikat och stänger sedan arkivet. Det förutsätter att du har tre certifikat att lägga till och ta bort från ett lokalt arkiv.

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

Kommentarer

Använd den här klassen för att arbeta med ett X.509-arkiv.

Important

Från och med .NET Framework 4.6 implementerar den här typen gränssnittet IDisposable. När du har använt typen bör du kassera den på ett direkt eller indirekt sätt. Om du vill ta bort typen direkt anropar du dess Dispose metod i ett try/catch block. Om du vill ta bort det indirekt använder du en språkkonstruktion som using (i C#) eller Using (i Visual Basic). Mer information finns i avsnittet "Använda ett objekt som implementerar IDisposable" i IDisposable gränssnittet.

För appar som riktar sig mot .NET Framework 4.5.2 och tidigare versioner implementerar klassen X509Store inte gränssnittet IDisposable och har därför ingen Dispose-metod.

Konstruktorer

Name Description
X509Store()

Initierar en ny instans av klassen med hjälp av X509Store det personliga certifikatarkivet för den aktuella användaren.

X509Store(IntPtr)

Initierar en ny instans av klassen med hjälp av X509Store ett Intptr-handtag till ett HCERTSTORE arkiv.

X509Store(StoreLocation)

Initierar en ny instans av klassen med hjälp av X509Store det personliga certifikatarkivet från det angivna butiksplatsvärdet.

X509Store(StoreName, StoreLocation, OpenFlags)

Initierar en ny instans av X509Store klassen med det angivna butiksnamnet och lagringsplatsvärdena och öppnar den sedan med de angivna flaggorna.

X509Store(StoreName, StoreLocation)

Initierar en ny instans av klassen med hjälp av X509Store angivna StoreName värden och StoreLocation värden.

X509Store(StoreName)

Initierar en ny instans av X509Store klassen med det angivna butiksnamnet från den aktuella användarens certifikatarkiv.

X509Store(String, StoreLocation, OpenFlags)

Initierar en ny instans av X509Store klassen med det angivna butiksnamnet och lagringsplatsvärdena och öppnar den sedan med de angivna flaggorna.

X509Store(String, StoreLocation)

Initierar en ny instans av X509Store klassen med ett angivet butiksnamn och en lagringsplats.

X509Store(String)

Initierar en ny instans av X509Store klassen med det angivna butiksnamnet.

Egenskaper

Name Description
Certificates

Returnerar en samling certifikat som finns i ett X.509-certifikatarkiv.

IsOpen

Hämtar ett värde som anger om instansen är ansluten till ett öppet certifikatarkiv.

Location

Hämtar platsen för X.509-certifikatarkivet.

Name

Hämtar namnet på X.509-certifikatarkivet.

StoreHandle

Hämtar ett IntPtr handtag till en HCERTSTORE butik.

Metoder

Name Description
Add(X509Certificate2)

Lägger till ett certifikat i ett X.509-certifikatarkiv.

AddRange(X509Certificate2Collection)

Lägger till en samling certifikat i ett X.509-certifikatarkiv.

Close()

Stänger ett X.509-certifikatarkiv.

Dispose()

Släpper de resurser som används av den här X509Store.

Equals(Object)

Avgör om det angivna objektet är lika med det aktuella objektet.

(Ärvd från Object)
GetHashCode()

Fungerar som standard-hash-funktion.

(Ärvd från Object)
GetType()

Hämtar den aktuella instansen Type .

(Ärvd från Object)
MemberwiseClone()

Skapar en ytlig kopia av den aktuella Object.

(Ärvd från Object)
Open(OpenFlags)

Öppnar ett X.509-certifikatarkiv eller skapar ett nytt arkiv, beroende på OpenFlags flagginställningar.

Remove(X509Certificate2)

Tar bort ett certifikat från ett X.509-certifikatarkiv.

RemoveRange(X509Certificate2Collection)

Tar bort ett intervall med certifikat från ett X.509-certifikatarkiv.

ToString()

Returnerar en sträng som representerar det aktuella objektet.

(Ärvd från Object)

Gäller för