X509Certificate – 密钥集不存在

我有一个使用WCF的WinForms应用程序,并作为参数传递给函数证书:

mySvcClient.SendDocument(cert.Export(X509ContentType.SerializedCert, "password")); ... 

在WCF服务中 ,我从字节数组重新创建了证书:

 public void SendDocument (byte[] binaryCert) { X509Certificate2 cert = new X509Certificate2(binaryCert, "password"); ... 

但是当使用证书签署xml时,我收到错误“Keyset不存在”:

 if (cert.HasPrivateKey) // WORKS!!! { signedXml.SigningKey = cert.PrivateKey; // THROW "keyset does not exist" EXCEPTION ... 

在我的电脑中,该应用程序100%工作! 但是在WebServer中,我收到了这个错误!

问题是:即使X509Certificate2从一个字节数组重新创建,我需要一些特殊权限才能访问私钥?

谢谢!

如果您使用的是Windows Server 2008或Windows 7,则需要具有读取私钥的权限。

  1. 使用FindPrivateKey工具查找路径。 例如:

FindPrivateKey My LocalMachine -n​​“CN = MyCert”-a

它返回路径:C:\ ProgramData \ Microsoft \ Crypto \ RSA \ MachineKeys [文件名]

  1. 转到该路径并打开文件属性

  2. 转到安全选项卡

  3. 点击“编辑”然后“添加”

  4. 在打开的对话框中写入:IIS AppPool \ [您的应用程序池名称],然后单击“确定”

现在,您的应用程序池有权读取此私钥。

我遇到过这个问题,我的证书有私钥,但是我收到了这个错误(“ Keyset不存在 ”)

原因:您的网站在“ 网络服务 ”帐户下运行或具有较少的权限。

解决方案:将应用程序池标识更改为“ 本地系统 ”, 重置IIS并再次检查。 如果它开始工作它是权限/较少权限问题,您也可以冒充然后使用其他帐户。

我面临着同样的问题,我不知道(对我来说很羞耻),但它有效:

 var certificate = new X509Certificate2(filePath, password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet); certificate.PrivateKey; // before: error "KeySet does not exist"! using (certificate.GetRSAPrivateKey()) { } // pure black magic certificate.PrivateKey; // after: just works! lol 

我希望有人能回答这个谜。

Vano Maisuradze回答有效。 如果您正在寻找FindPrivateKey工具,它将包含在Windows Communication Foundation(WCF)和Windows Workflow Foundation(WF).NET Framework 4示例中,可以在此处找到: http : //www.microsoft.com/en-我们/下载/ confirmation.aspx?ID = 21459

下载并解压缩后,在Visual Studio中打开项目:WF_WCF_Samples \ WCF \ Setup \ FindPrivateKey \ CS并进行编译。 然后打开命令提示符并导航到:WF_WCF_Samples \ WCF \ Setup \ FindPrivateKey \ CS \ bin

然后继续Vano Maisuradze回答

我认为问题是您需要将密钥添加到计算机的证书存储区。

默认情况下,应用程序池标识帐户无权访问证书存储。

您可以根据Vaibhav.Inspired的指示更改为Network Services帐户,也可以访问证书。

要允许访问,请执行以下命令:

WinHttpCertCfg.exe -g -c LOCAL_MACHINE \ MY -s“IssuedToName”-a“AccountName”

笔记:

 - The tool may need to be installed first. The setup will place the tool at `C:\Program Files (x86)\Windows Resource Kits\Tools\WinHttpCertCfg.exe`. - `IssuedName` is the issuer property of the certificate that the application will attempt to access - The command must be run from command prompt with elevated privileges 

参考: https : //support.microsoft.com/en-us/help/901183/how-to-call-a-web-service-by-using-a-client-certificate-for-authentica第2步

此外,您还需要在安装证书时启用Mark this key as exportable选项。