Share via


X509CertificateStore.CurrentUserStore Method

Creates a new X509CertificateStore using the specified name, a location of CurrentUser, and a store provider of System.

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

Usage

'Usage
Dim storeName As String
Dim returnValue As X509CertificateStore
returnValue = X509CertificateStore.CurrentUserStore(storeName)

Syntax

'Declaration
Public Shared Function CurrentUserStore( _
    ByVal storeName As String _
) As X509CertificateStore
public static X509CertificateStore CurrentUserStore(
    string storeName
);
public: static X509CertificateStore^ CurrentUserStore(
    String^ storeName
);
public static X509CertificateStore CurrentUserStore(
    System.String storeName
);
public static function CurrentUserStore(
     storeName : String
) : X509CertificateStore;

Parameters

Return Value

An X509CertificateStore with its X509CertificateStore.StoreLocation value set to CurrentUser, its X509CertificateStore.StoreProvider value set to System, and its name set to storeName.

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;
}

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

X509CertificateStore Class
Microsoft.Web.Services2.Security.X509 Namespace

Other Resources

X509CertificateStore Members