你至少有两个问题的可能性......
首先...
除非使用密码访问,否则您的客户端证书文件不能包含私钥。您应该使用带有密码的 PKCS #12 (*.pfx) 证书,以便您的客户端可以访问私钥。正如其他人已经发布的那样,您的客户端代码在打开证书时必须提供密码。有几种创建方法,最简单的方法是使用以下命令行首先生成证书,然后使用 MMC 证书管理器导出证书私钥:
Process p = Process.Start(
"makecert.exe",
String.Join(" ", new string[] {
"-r",// Create a self signed certificate
"-pe",// Mark generated private key as exportable
"-n", "CN=" + myHostName,// Certificate subject X500 name (eg: CN=Fred Dews)
"-b", "01/01/2000",// Start of the validity period; default to now.
"-e", "01/01/2036",// End of validity period; defaults to 2039
"-eku",// Comma separated enhanced key usage OIDs
"1.3.6.1.5.5.7.3.1," +// Server Authentication (1.3.6.1.5.5.7.3.1)
"1.3.6.1.5.5.7.3.2", // Client Authentication (1.3.6.1.5.5.7.3.2)
"-ss", "my",// Subject's certificate store name that stores the output certificate
"-sr", "LocalMachine",// Subject's certificate store location.
"-sky", "exchange",// Subject key type <signature|exchange|<integer>>.
"-sp",// Subject's CryptoAPI provider's name
"Microsoft RSA SChannel Cryptographic Provider",
"-sy", "12",// Subject's CryptoAPI provider's type
myHostName + ".cer"// [outputCertificateFile]
})
);
第二...
您的下一个问题将是服务器端。服务器必须允许此证书。你有正确的逻辑,但是在错误的一端,将这条线移动到处理请求的 Web 服务器。如果不能,则必须将上面保存的“.cer”文件带到服务器并将其添加到服务器计算机的信任列表中:
ServicePointManager.ServerCertificateValidationCallback = (a,b,c,d) => true;