views:

38

answers:

1

Working on a winform application and connecting to socket, I am able to create SSLStream and authenticate. using following code

// Authenticate ourself as a client.
                    this.sslStream.AuthenticateAsClient(SSL_TARGET_HOST);

Now sometime application throw AuthenticationException if on client machine certificate is not installed.

I wonder if there is a way to check that a particular certificate is installed on client machine before calling to connect?

+1  A: 

You can use the X509Store class to determine what certificates are installed in a particular certificate store. There are various ways you can look for certificates (e.g. subject name, issuer name, serial number etc).

For example, to open the current user's personal store and search for a certificate by subject name:

X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

try
{
    store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

    X509Certificate2Collection foundCerts = store.Certificates.Find(X509FindType.FindBySubjectName, "MY CERTIFICATE SUJECT NAME", true);

    if (foundCerts.Count == 0)
    {
        // Cert not found
    }
    else
    {   
        X509Certificate2 cert = foundCerts[0]; // Get first matching certificate
    }
}
finally
{
    store.Close();
}
Cocowalla