【问题标题】:How to determine which personal certificate comes from hardware device in C#?如何确定哪个个人证书来自 C# 中的硬件设备?
【发布时间】:2013-12-18 01:31:30
【问题描述】:

假设我有多个当前用户的个人证书。但只有一张证书属于阿拉丁 eToken。

我想确定哪个证书属于 Aladdin eToken。

我应该为此使用X509StoreX509Crtificate 吗?

我应该尝试 eToken SDK 吗?

【问题讨论】:

    标签: c# x509certificate cryptoapi e-token


    【解决方案1】:

    这可以帮助您找到证书。 它创建位置和商店列表并提供证书数量。 使用令牌和令牌运行它可以帮助您找到它在哪里:

    public static string ListCertificatesCount()
    {
        string output = "";
        foreach (StoreName st in (StoreName[])Enum.GetValues(typeof(StoreName)))
        {
            foreach (StoreLocation loc in (StoreLocation[])Enum.GetValues(typeof(StoreLocation)))
            {
                string line = "StoreName " + Enum.GetName(typeof(StoreName), st) + ", StoreLocation " + Enum.GetName(typeof(StoreLocation), loc) + ": Count: ";
                try
                {
                    using (X509Store keyStore = new X509Store(st, loc))
                    {
                        keyStore.Open(OpenFlags.ReadOnly);
                        line += keyStore.Certificates.Count;
                        keyStore.Close();
                    }
                }
                catch (Exception ex)
                {
                    line += "Fail: " + ex.Message;
                }
                output += line + Environment.NewLine;
            }
        }
        return output;
    }
    

    【讨论】:

      【解决方案2】:

      这取决于 Aladdin eToken 如何签署证书。如果它颁发的 X509 证书的 Issuer 字段设置为可识别的内容(例如 Aladdin eToken),那么您应该能够以这种方式找到证书。

      // Get the MY store for the current user
      X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
      
      store.Open(OpenFlags.ReadOnly);
      
      X509Certificate2Collection certs = 
          store.Certificates.Find(X509FindType.FindByIssuerName,
                                  "Aladdin eToken");
      

      这应该会为您提供所有在颁发者名称中包含字符串“Aladdin eToken”的证书。如果您需要使用不同的标准来识别证书,还有大量其他有效参数,您可以通过 Certificates 集合的 Find 方法来获取匹配项。

      例如,如果您要查找特定证书,您可以FindByThumbprintFindBySerialNumber

      【讨论】:

      • eToken 是持有证书的硬件,它不是证书颁发者。所以不幸的是,你的答案不适用。问题是如何找出证书存储在令牌上而不是注册表中。
      • 抱歉,好像发行人是默认发行人。
      • @skfd 其他字段呢?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 2015-01-08
      • 2013-06-17
      • 2017-11-16
      • 1970-01-01
      • 2020-08-13
      相关资源
      最近更新 更多