C# 当客户端应用程序已发布但在本地运行时,无法从存储中找到证书

C# 当客户端应用程序已发布但在本地运行时,无法从存储中找到证书,c#,certificate,C#,Certificate,当我在本地运行此操作时,它会从智能卡中成功找到证书,读取证书并在文档上签名 如果我在客户端上发布应用程序的这一部分,使用https在IIS上运行它,然后在localhost上运行整个应用程序,并将其连接到客户端应用程序它成功连接到它,但证书数为0 不幸的是,因为它是客户端,我只能使用日志,而不能使用调试 到目前为止,我已经确定没有与证书上的指纹匹配的证书。但是,它通过thumprint在本地找到证书 我已尝试将X509StoreStoreName更改为StoreName.Root,StoreNa

当我在本地运行此操作时,它会从
智能卡
中成功找到
证书
,读取证书并在文档上签名

如果我在
客户端
上发布应用程序的这一部分,使用
https
IIS
上运行它,然后在
localhost
上运行整个
应用程序
,并将其连接到
客户端应用程序
它成功连接到它,但
证书数
为0

不幸的是,因为它是
客户端
,我只能使用
日志
,而不能使用
调试

到目前为止,我已经确定没有与
证书
上的
指纹
匹配的
证书
。但是,它通过
thumprint
在本地找到
证书

我已尝试将
X509Store
StoreName
更改为
StoreName.Root
StoreName.CertificationAuthority
,但它始终找不到我要查找的
证书

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

如果您有任何建议,我们将不胜感激。

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


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

您好。我无法导出带有私钥的证书,因为它来自智能卡。它现在的工作原理-它使用IDPrimePKCS11.dll从智能卡读取证书,使用私钥使用数字签名进行签名。在localhost上,它们都可以工作。一旦我发表了,它就不会。。可能是对文件夹的权限?我在IIS上启用了SSL,当我在第一个实例上运行它时,它在浏览器中请求证书,我选择了证书,输入了PIN,但仍然不起作用。日志显示store.localuser中的证书数为0。我只是想知道。。。IIS是否有办法从智能卡获取证书或以某种方式将其添加到本地存储?当然,store.localuser中的证书数将为零,因为证书位于智能卡上。我仍然认为这是一个权限问题,IIS用户没有读取智能卡或文件本身的权限。