X509CertificateCollection Class

Represents a collection of X509Certificate objects.

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

Usage

'Usage
Dim x509CertificateCollection1 As New X509CertificateCollection()

Syntax

'Declaration
Public Class X509CertificateCollection
    Inherits CollectionBase
public class X509CertificateCollection : CollectionBase
public ref class X509CertificateCollection : public CollectionBase
public class X509CertificateCollection extends CollectionBase
public class X509CertificateCollection extends CollectionBase

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

Inheritance Hierarchy

System.Object
  System.Collections.CollectionBase
    Microsoft.Web.Services2.Security.X509.X509CertificateCollection

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

X509CertificateCollection Members