【问题标题】:Check SSL Certificate installed, on client Side检查客户端安装的 SSL 证书
【发布时间】:2010-08-20 15:42:09
【问题描述】:

在一个 winform 应用程序上工作并连接到套接字,我能够创建 SSLStream 并进行身份验证。使用以下代码

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

如果在客户端机器上没有安装证书,现在有时应用程序会抛出 AuthenticationException。

我想知道是否有办法在调用连接之前检查客户端计算机上是否安装了特定证书?

【问题讨论】:

  • 为什么不直接使用异常作为证书不受信任的指示?在 SSL 中,您甚至在握手开始之前都看不到服务器证书,此时阻止异常发生为时已晚。

标签: c# ssl-certificate


【解决方案1】:

您可以使用X509Store 类来确定特定证书存储中安装了哪些证书。您可以通过多种方式查找证书(例如主题名称、颁发者名称、序列号等)。

例如,打开当前用户的个人商店并按主题名称搜索证书:

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

【讨论】:

    猜你喜欢
    • 2017-04-28
    • 2013-08-04
    • 2010-10-16
    • 2012-02-15
    • 2017-06-01
    • 2012-10-18
    • 2012-07-19
    • 2013-10-01
    • 1970-01-01
    相关资源
    最近更新 更多