0

我有一个来自 Thawte 的代码签名证书,用于动态生成和签署 ClickOnce 部署清单。问题是,当我们重新启动 IIS 或重新导入签名证书时,生成和签署这些清单的 ASP.NET 应用程序工作正常,但随着时间的推移,以下函数无法找到证书:

private static X509Certificate2 GetSigningCertificate(string thumbprint)
{
    X509Store x509Store = new X509Store(StoreLocation.CurrentUser);
    try
    {
        x509Store.Open(OpenFlags.ReadOnly);
        X509Certificate2Collection x509Certificate2Collection = x509Store.Certificates.Find(
            X509FindType.FindByThumbprint, thumbprint, false);

        if (x509Certificate2Collection.Count == 0)
            throw new ApplicationException(
                "SigningThumbprint returned 0 results.  Does the code signing certificate exist in the personal store?",
                null);

        if (x509Certificate2Collection.Count > 1)
            throw new ApplicationException(
                "SigningThumbprint returned more than 1 result.  This isn't possible", null);

        var retval = x509Certificate2Collection[0];

        if(retval.PrivateKey.GetType() != typeof(RSACryptoServiceProvider))
            throw new ApplicationException("Only RSA certificates are allowed for code signing");

        return retval;
    }
    finally
    {
        x509Store.Close();
    }
}

最终,应用程序开始抛出找不到证书的错误。我很困惑,因为我认为证书安装正确(或大部分正确),因为它确实在我们启动 ASP.NET 应用程序时找到了证书,但在某些时候我们点击了 Count==0 分支,但事实并非如此:证书位于应用程序池用户的“当前用户\个人”证书存储中。

问题:为什么证书会突然“消失”或找不到?

4

1 回答 1

1

我们自己(痛苦地)想通了。

如果要从 ASP.NET 应用程序中使用证书,则需要将证书安装在 LocalMachine 存储中,并且应用程序池帐户使用 WinHttpCertCfg 或 CACLS.exe 读取证书的权限。使用运行应用程序池的帐户的 CurrentUser 存储会导致问题。我猜测存在某种竞争条件,或者从未在交互式登录会话中运行的用户访问 CurrentUser 存储并不完全酷。

起初我们无法执行此操作,因为我们正在调用 MAGE 工具来执行 ClickOnce 部署清单创建/签名,这需要代码签名证书位于 CurrentUser\My 商店中。但是,我们通过以下方式消除了对 MAGE 的需求:a) 从模板文件创建清单并替换我们需要替换的值;b) 通过通过 BuildTasks 中存在的反射调用 MAGE 调用代码对清单进行签名。 v3.5 DLL。因此,我们可以更好地控制我们使用什么证书来签署清单,并且可以将它放在我们想要的任何地方。否则,如果我们没有走一点“低级”,我们就会被卡住。

于 2011-08-18T22:24:58.247 回答