无法使用C#代码

时间:2017-02-08 07:34:20

标签: c# build-server certificate-store

无法使用C#代码访问证书。证书已安装在Build Server上。我使用下面的代码访问证书,这在我的情况下不起作用:

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

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

StackTrace @ col.Count:

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

谢谢

0 个答案:

没有答案