证书 'CN=ds.com' 必须有私钥。该进程必须具有私钥的访问权限

The certificate 'CN=ds.com' must have a private key. The process must have access rights for the private key

经过大量搜索并尝试其他人的答案后,每个解决方案 - 仍然没有成功

Short preface : response request from my machine to itself - does work with certificate. From another computer to my machine - it doesn't.

在我的机器中-我创建了 2 个证书:public 密钥和私钥通过:

makecert -r -pe -n "CN=ds.com" -b 01/01/2018 -e 01/01/2020 
-sky exchange Server.cer -sv Server.pvk

然后:

pvk2pfx.exe -pvk Server.pvk -spc Server.cer -pfx Server.pfx

(取自here

我已经将它们 安装在我机器的商店中(通过 mmc):

我正在使用这个简单的 WCF config,我在 我的 机器上托管在 WAS IIS 中。

当我使用这个简单的代码调用该服务(从我的机器到它自己)时:

  WSHttpBinding myBinding = new WSHttpBinding();
            myBinding.Security.Mode = SecurityMode.Message;
            myBinding.Security.Message.ClientCredentialType =MessageCredentialType.Certificate;
            EndpointAddress ea = new
                EndpointAddress("http://ds.com/Service1.svc/HelloWorldService");

            var client   = new HelloWorldServiceClient(myBinding, ea);

            client.ClientCredentials.ClientCertificate.SetCertificate(
                StoreLocation.CurrentUser,
                StoreName.Root,
                X509FindType.FindByThumbprint,
                "9394f570069e7af263ef7ca5a46a5bcab9f68659");
    Console.WriteLine(client.GetMessage("Mike Liu"));
    Console.ReadLine();
    client.Close();

— 我确实得到了结果:

太好了。

现在让我们去 另一台 我安装在 的计算机 将 public 密钥证书进入受信任的根目录.

但是-现在当我访问我的计算机时(使用相同的代码^)-我收到以下错误:

The certificate 'CN=ds.com' must have a private key. The process must have access rights for the private key.

即使我关闭了我计算机上的 WAS IIS 服务,我仍然在另一台计算机上看到错误。

看来问题只出在另一台机器上。我已经在另一台机器上设置了权限:

C:\ProgramData\Microsoft\Crypto <--------Everyone: full control +inheritance

问题:

我在这里错过了什么?为什么它会在客户端计算机中查找私钥。它不应该有私钥。私有应仅驻留在服务计算机中。 (这是我的机器)。

Full stack trace image (at the other machine)

在您的示例中,您在服务器和客户端上使用相同的证书。这不应该是这样的。

为了确保客户端请求的安全,您必须使用客户端证书对请求进行签名,并使用服务器证书对服务器响应进行签名。

所以在客户端你应该有:

  • 个人证书库中客户端证书的私钥(和 public)
  • public 可信根权限内的服务器证书密钥

在服务器上你应该有相反的:

  • 个人证书存储中服务器证书的私钥(和 public)
  • public 受信任的根权限内的客户端证书密钥

你可以看例子here and read more about it here

有关 HTTPS 协议的详细信息在 article 中有很好的描述。

这可能是由于 2 个可能的原因。

  1. 证书当前用户存储中没有私钥可用(personal/trusted 人)
  2. IIS 应用程序池无权访问(当前用户存储)的私钥

启用wcf trace,你会在图中提到的日志中发现错误。

对于 1) 安装私钥 (pfx) 第二次在应用程序池中提供身份,如下图所示。