简体   繁体   中英

Check SSL Certificate installed, on client Side

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?

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 (eg 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM