X509CertificateStore.FindCertificateBySubjectName Method

Finds a X509Certificate object in the store using the certificate's name value.

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

Usage

'Usage
Dim subjectstring As String
Dim returnValue As X509CertificateCollection
Dim x509CertificateStore1 As X509CertificateStore
returnValue = x509CertificateStore1.FindCertificateBySubjectName(subjectstring)

Syntax

'Declaration
Public Function FindCertificateBySubjectName( _
    ByVal subjectstring As String _
) As X509CertificateCollection
public X509CertificateCollection FindCertificateBySubjectName(
    string subjectstring
);
public: X509CertificateCollection^ FindCertificateBySubjectName(
    String^ subjectstring
);
public X509CertificateCollection FindCertificateBySubjectName(
    System.String subjectstring
);
public function FindCertificateBySubjectName(
     subjectstring : String
) : X509CertificateCollection;

Parameters

  • subjectstring
    a string that contains the certificate's name value

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 subject name.

Private Function GetCertificateUsingSubjectName(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 subjectName As String
    If IsTokenForSigning Then
        ' Specify the subject name of the client's certificate.
        subjectName = "C=US, S=WA, L=Redmond, O=Microsoft Corporation, OU=Web Services, CN=Don Funk - Client, E=jemand@Microsoft.com"
        ' Specify the subject name of the server's certificate.
    Else
        subjectName = "C=US, S=WA, L=Redmond, O=Microsoft Corporation, OU=Web Services, CN=Don Funk - Server, E=jemand@Microsoft.com"
    End If
    ' Search the certificate store for a certifacte based on
    ' the certificate's subject name.
    Dim certs As X509CertificateCollection = store.FindCertificateBySubjectName(subjectName)

    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 'GetCertificateUsingSubjectName
private Microsoft.Web.Services2.Security.X509.X509Certificate GetCertificateUsingSubjectName(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;
    string subjectName;
    if (IsTokenForSigning)
        // Specify the subject name of the client's certificate.
        subjectName = "C=US, S=WA, L=Redmond, O=Microsoft Corporation, OU=Web Services, CN=Don Funk - Client, E=jemand@Microsoft.com";
    else
        // Specify the subject name of the server's certificate.
        subjectName = "C=US, S=WA, L=Redmond, O=Microsoft Corporation, OU=Web Services, CN=Don Funk - Server, E=jemand@Microsoft.com";

    // Search the certificate store for a certifacte based on
    // the certificate's subject name.
    X509CertificateCollection certs =
        store.FindCertificateBySubjectName(subjectName);

    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