Share via


X509CertificateStore Class

Provides access to an X.509 certificate store on the local computer.

Namespace: Microsoft.Web.Services2.Security.X509
Assembly: Microsoft.Web.Services2 (in microsoft.web.services2.dll)

Usage

'Usage
Dim provider As X509CertificateStore.StoreProvider
Dim location As X509CertificateStore.StoreLocation
Dim storeName As String
Dim x509CertificateStore1 As New X509CertificateStore(provider, location, storeName)

Syntax

'Declaration
Public Class X509CertificateStore
    Implements IDisposable
public class X509CertificateStore : IDisposable
public ref class X509CertificateStore : public IDisposable
public class X509CertificateStore implements IDisposable
public class X509CertificateStore implements IDisposable

Example

The following code example opens the My certificate store for the currently logged in user and then searches for a particular X.509 certificate using the certificate's key identifier.

Private Function GetCertificateUsingKeyIdentifer(ByVal IsTokenForSigning As Boolean) As Microsoft.Web.Services2.Security.X509.X509Certificate
    ' Open up the My certificate store for the currently logged
    ' in user.
    Dim store As X509CertificateStore
    store = X509CertificateStore.CurrentUserStore(X509CertificateStore.MyStore)
    Dim open As Boolean = store.OpenRead()


    Dim cert As Microsoft.Web.Services2.Security.X509.X509Certificate = Nothing
    Dim certKeyID() As Byte
    If IsTokenForSigning Then
        certKeyID = New Byte() {&H48, &H1B, &HE8, &HEC, &HBD, &H32, &HC, &HD6, &H39, &HA8, &H9B, &HCE, &HEA, &H5A, &H2A, &HE4, &H66, &H76, &H62, &H42}
    Else
        certKeyID = New Byte() {&HE8, &H8F, &HE1, &H8A, &H62, &H6, &HD6, &H1A, &H85, &HF6, &H5A, &H26, &H81, &H0, &H56, &H29, &HB1, &H8A, &H29, &H47}
    End If
    ' Search for a certifacte based on the
    ' certificate's key identifier.
    Dim certs As X509CertificateCollection = store.FindCertificateByKeyIdentifier(certKeyID)

    If certs.Count > 0 Then
        ' Obtain the first matching certificate.
        cert = CType(certs(0), Microsoft.Web.Services2.Security.X509.X509Certificate)

        ' No certificates matched the search criteria.
    Else
        cert = Nothing
    End If
    ' Close the X.509 certificate store.
    If Not (store Is Nothing) Then

        '
        'Close the X.509 certificate store.
        If (Not store Is Nothing) Then
            store.Close()
        End If

    End If
    Return cert
End Function 'GetCertificateUsingKeyIdentifer
private Microsoft.Web.Services2.Security.X509.X509Certificate GetCertificateUsingKeyIdentifer(bool IsTokenForSigning)
{
    // Open up the My certificate store for the currently logged
    // in user.
    X509CertificateStore store;
    store = X509CertificateStore.CurrentUserStore(
        X509CertificateStore.MyStore);
    bool open = store.OpenRead();

    Microsoft.Web.Services2.Security.X509.X509Certificate cert = null;
    byte[] certKeyID;
    if (IsTokenForSigning)
        certKeyID = new byte[]{0x48, 0x1b, 0xe8, 0xec, 0xbd, 0x32, 0xc, 0xd6, 0x39, 0xa8, 0x9b, 0xce, 0xea, 0x5a, 0x2a, 0xe4, 0x66, 0x76, 0x62, 0x42};
    else
        certKeyID = new byte[]{0xe8, 0x8f, 0xe1, 0x8a, 0x62, 0x6, 0xd6, 0x1a, 0x85, 0xf6, 0x5a, 0x26, 0x81, 0x0, 0x56, 0x29, 0xb1, 0x8a, 0x29, 0x47};

    // Search for a certifacte based on the
    // certificate's key identifier.
    X509CertificateCollection certs =
        store.FindCertificateByKeyIdentifier(certKeyID);
        
    if (certs.Count > 0)
        // Obtain the first matching certificate.
        cert = ((Microsoft.Web.Services2.Security.X509.X509Certificate) certs[0]);
    else
        // No certificates matched the search criteria.
        cert = null;

    // Close the X.509 certificate store.
    if (store != null) { store.Close(); }

    return cert;
}

Remarks

A certificate store can have numerous certificates, possibly issued from a number of different certification authorities.

Certificate stores can be created for a user, a computer, or a service according to the purpose for which the certificates were issued or by using their logical storage categories.

Inheritance Hierarchy

System.Object
  Microsoft.Web.Services2.Security.X509.X509CertificateStore

Thread Safety

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

Platforms

Development Platforms

Windows XP Home Edition, Windows XP Professional, Windows Server 2003, Windows Longhorn, and Windows 2000

Target Platforms

Windows 2000, Windows 2000 Server, Windows 2000 Advanced Server, Windows XP Home Edition, Windows XP Professional, Windows Server 2003, Windows Longhorn, Pocket PC, Windows CE, Smart Phone

See Also

Reference

Microsoft.Web.Services2.Security.X509 Namespace

Other Resources

X509CertificateStore Members