Export (0) Print
Expand All

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
Dim certHash() As Byte
Dim returnValue As X509CertificateCollection
Dim x509CertificateStore1 As X509CertificateStore
returnValue = x509CertificateStore1.FindCertificateByHash(certHash)

public X509CertificateCollection FindCertificateByHash(
	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.

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

Exception typeCondition
System.InvalidOperationException

The store is not yet opened.

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

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
Show:
© 2014 Microsoft