首页 > 解决方案 > 客户端应用程序发布但在本地工作时无法从商店中找到证书

问题描述

当我在本地运行它时,它成功地certificate从 中找到smart card,读取它并签署文档。

如果我在 a 上发布应用程序的这一部分,在using上client side运行它,然后我运行整个并将其连接到它成功连接到它,但数量为0。IIShttpsapplicationlocalhostclient side appcertificates

不幸的是,因为它是client side我只能使用logs而不是debug.

到目前为止,我已经确定没有certsthumbprint. certificate但是,再次在本地找到certificate.thumprint

我试图将 , 更改X509Store StoreNameStoreName.Root, StoreName.CertificationAuthority,但它永远找不到cert我正在寻找的。

    public (InvoiceResult resultValue, X509Certificate2 cert) GetDefaultCertificateStoredOnTheCard()
    {

        var resultValue = InvoiceResult.Success;
        using X509Store x509Store = new X509Store("MY", StoreLocation.CurrentUser);

        X509Store store = x509Store;
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

        X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, true);

        // by thumbprint, there is only one
        certs = certs.Find(X509FindType.FindByThumbprint, Settings.Default.Thumbprint, true);

        _log.TraceInformation($"Locations of certs in store after opening flags is {certs.Count}!");

        if (certs.Count == 0)
        {
            resultValue = InvoiceResult.CannotFindSignature;
            // throw new ArgumentException("Please insert smart card to obtain certificate.");
        }
        X509Certificate2 cert = certs[0];
        if (cert.HasPrivateKey)
        {
            // software cert
            _ = cert.PrivateKey as RSACryptoServiceProvider;

        }
        else
        {
            // certificate from smartcard
            CspParameters csp = new CspParameters(1, "Microsoft Base Smart Card Crypto Provider")
            {
                Flags = CspProviderFlags.UseDefaultKeyContainer
            };
            _ = new RSACryptoServiceProvider(csp);
        }
        _log.TraceInformation($"GetDefaultCerticateStoredOnTheCard method gets values {cert}.");
        _log.TraceInformation($"GetDefaultCerticateStoredOnTheCard method gets values {resultValue}.");

        return (resultValue, cert);
    }

任何建议将不胜感激。

标签: c#certificate

解决方案


最有可能的是,运行您的应用程序的帐户没有从商店读取证书的权限。

在 MMC 中打开您的证书存储。右键单击证书并选择所有任务 > 管理私钥(注意:我的机器上安装了 Windows 7;在其他版本的 Windows 中可能会有所不同)。然后,您可以将读取权限分配给正确的用户帐户。


推荐阅读