Export (0) Print
Expand All

X509Store Class

Represents an X.509 store, which is a physical store where certificates are persisted and managed. This class cannot be inherited.

System.Object
  System.Security.Cryptography.X509Certificates.X509Store

Namespace:  System.Security.Cryptography.X509Certificates
Assembly:  System (in System.dll)

'Declaration
Public NotInheritable Class X509Store _
	Implements IDisposable

The X509Store type exposes the following members.

  NameDescription
Public methodX509StoreInitializes a new instance of the X509Store class using the personal certificates of the current user store.
Public methodX509Store(IntPtr)Initializes a new instance of the X509Store class using an Intptr handle to an HCERTSTORE store.
Public methodX509Store(StoreLocation)Initializes a new instance of the X509Store class using the specified StoreLocation value.
Public methodX509Store(StoreName)Initializes a new instance of the X509Store class using the specified StoreName value.
Public methodX509Store(String)Initializes a new instance of the X509Store class using the specified store name.
Public methodX509Store(StoreName, StoreLocation)Initializes a new instance of the X509Store class using the specified StoreName and StoreLocation values.
Public methodX509Store(String, 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.
Top

  NameDescription
Public propertyCertificatesReturns a collection of certificates located in an X.509 certificate store.
Public propertyLocationGets the location of the X.509 certificate store.
Public propertyNameGets the name of the X.509 certificate store.
Public propertyStoreHandleGets an IntPtr handle to an HCERTSTORE store.
Top

  NameDescription
Public methodAddAdds a certificate to an X.509 certificate store.
Public methodAddRangeAdds a collection of certificates to an X.509 certificate store.
Public methodCloseCloses an X.509 certificate store.
Public methodDisposeReleases the resources used by this X509Store.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodOpenOpens an X.509 certificate store or creates a new store, depending on OpenFlags flag settings.
Public methodRemoveRemoves a certificate from an X.509 certificate store.
Public methodRemoveRangeRemoves a range of certificates from an X.509 certificate store.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

Use this class to work with an X.509 store.

Important noteImportant

This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic.

This section contains two examples. 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.

Example 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.

The example creates an X509Store object for each combination of standard name and standard location. It calls the Open method with the OpenFlags.OpenExistingOnly flag, which opens the physical store only if it already exists. If the physical store exists, the example uses the Name, Location, and Certificates properties to display the number of certificates in the store.

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

Example 2

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.

Imports System
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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2015 Microsoft