X509CertificateStore.FindCertificateByHash Method

Finds a X509Certificate object in the store using the certificate's (SHA-1) hash value.

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

Usage

'Usage
Dim certHash() As Byte
Dim returnValue As X509CertificateCollection
Dim x509CertificateStore1 As X509CertificateStore
returnValue = x509CertificateStore1.FindCertificateByHash(certHash)

Syntax

'Declaration
Public Function FindCertificateByHash( _
    ByVal certHash() As Byte _
) As X509CertificateCollection
public X509CertificateCollection FindCertificateByHash(
    byte[] certHash
);
public: X509CertificateCollection^ FindCertificateByHash(
    array<Byte>^ certHash
);
public X509CertificateCollection FindCertificateByHash(
    ubyte[] certHash
);
public function FindCertificateByHash(
     certHash : Byte[]
) : X509CertificateCollection;

Parameters

  • certHash
    a byte array that contains the hash value of the X509Certificate to be searched

Return Value

A X509CertificateCollection that contains the search result.

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 certificate's hash value, which is also known as a thumbprint.

Private Function GetCertificateUsingHash(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 certHash() As Byte
    If IsTokenForSigning Then
        ' Populate a byte array with the certificate's hash, which is also
        ' known as its thumbprint.
        certHash = New Byte() {&H98, &HEC, &H8, &H4B, &HA5, &H7A, &H6C, &H2F, &H39, &H26, &HB3, &HA, &H58, &HBF, &H65, &H25, &H61, &HC5, &H64, &H59}
        ' Populate a byte array with the certificate's hash, which is also
        ' known as its thumbprint.
    Else
        certHash = New Byte() {&H5E, &H17, &HC6, &H14, &H8C, &HDC, &H86, &H85, &H81, &H45, &H3E, &HE2, &H66, &HD6, &H31, &HB2, &H92, &H23, &H3D, &HEB}
    End If
    ' Search the certificate store for a certifacte based on
    ' the certificate's hash.
    Dim certs As X509CertificateCollection = store.FindCertificateByHash(certHash)

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


    Else
        ' No certificates matched the search criteria.
        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 'GetCertificateUsingHash
private Microsoft.Web.Services2.Security.X509.X509Certificate GetCertificateUsingHash(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[] certHash;
    if (IsTokenForSigning)
        // Populate a byte array with the certificate's hash, which is also
        // known as its thumbprint.
        certHash = new byte[]{0x98, 0xec, 0x08, 0x4b, 0xa5 ,0x7a, 0x6c, 0x2f,0x39 ,0x26 ,0xb3 ,0x0a ,0x58, 0xbf ,0x65 ,0x25, 0x61, 0xc5, 0x64 ,0x59};
    else
        // Populate a byte array with the certificate's hash, which is also
        // known as its thumbprint.
        certHash = new byte[]{0x5e, 0x17, 0xc6, 0x14, 0x8c, 0xdc, 0x86, 0x85, 0x81, 0x45, 0x3e, 0xe2, 0x66, 0xd6, 0x31, 0xb2, 0x92, 0x23, 0x3d, 0xeb};

    // Search the certificate store for a certifacte based on
    // the certificate's hash.
    X509CertificateCollection certs =
        store.FindCertificateByHash(certHash);

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

Exceptions

Exception type Condition
System.InvalidOperationException

The store is not yet opened.

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