2

无法使用C#代码访问证书。证书已安装在构建服务器上。我正在使用以下代码访问证书,这在我的情况下不起作用:

public static X509Certificate2 FindCertificateByThumbprint(string findValue)
{
    X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    try
    {
        store.Open(OpenFlags.ReadOnly);
        X509Certificate2Collection col = store.Certificates.Find(X509FindType.FindByThumbprint, findValue, false);
        log.Info("X509Certificate2Collection col count : " + col.Count);
        log.Info(col.Count == 0 ? "Could not find the certificate" : "Certificate Retrieved");
        if (col == null || col.Count == 0)
            return null;
        return col[0];
    }
    catch(Exception ex)
    {
        log.Info(ex.StackTrace.ToString());
        log.Info(ex.Message);
        log.Info(ex.InnerException);
        return null;
    }
    finally
    {
        store.Close();
    }
}

col 计数返回为 0。当X509Certificate2Collection我在构建服务器上调试代码时,我能够检索证书。但无法从本地机器中检索相同的内容。

StackTrace@ col.Count:

at System.Collections.CollectionBase.System.Collections.IList.get_Item(Int32 index)
at System.Security.Cryptography.X509Certificates.X509Certificate2Collection.get_Item(Int32 index)

谢谢

4

0 回答 0