C# 无法读取X509Certificate2上的私钥

C# 无法读取X509Certificate2上的私钥,c#,.net,windows,powershell-4.0,C#,.net,Windows,Powershell 4.0,Win 8.1 64位。净额4.5。Powershell 4.0。(尽管我认为这个问题一般与.NET有关,而不是特定于Powershell。) 我生成一个自签名证书,如下所示: New-SelfSignedCertificate -DnsName 'TheIncredibleHulk' -CertStoreLocation Cert:\CurrentUser\My 然后我检索证书: $Cert = Get-Item Cert:\CurrentUser\My\9E5A8322B890DA1247

Win 8.1 64位。净额4.5。Powershell 4.0。(尽管我认为这个问题一般与.NET有关,而不是特定于Powershell。)

我生成一个自签名证书,如下所示:

New-SelfSignedCertificate -DnsName 'TheIncredibleHulk' -CertStoreLocation Cert:\CurrentUser\My
然后我检索证书:

$Cert = Get-Item Cert:\CurrentUser\My\9E5A8322B890DA1247BD98BDAB288CA9D11CF99E
证书具有私钥:

$Cert.HasPrivateKey
True
还是这样

$Cert.PrivateKey
(null)

为什么
PrivateKey
成员为空?如何检索私钥?

对于在这一过程中发生的任何其他人,我怀疑问题与新的SelfSignedCertificate使用的存储提供程序有关(请参阅,其中讨论了Microsoft软件密钥存储提供程序和.NET类的问题)。上有一个powershell脚本可用于创建自签名证书,并默认使用其他存储提供程序。

让我找到了一个解决方法。该问题是由默认提供程序引起的,但在中,您可以指定提供程序,如下所示

New-SelfSignedCertificate -DnsName HulkSmash -CertStoreLocation Cert:\LocalMachine\My  -KeyAlgorithm RSA -KeyLength 2048 -KeyExportPolicy Exportable -KeyProtection None -Provider 'Microsoft Enhanced RSA and AES Cryptographic Provider'

我认为这是在使用PSPKI()?@Kev不,没有使用自定义模块。有必要向进行访问的用户授予对私钥的访问权限。在certmgr.msc中,右键单击证书,执行“所有任务”->管理私钥并授予访问权限。感谢@JohnSaunders,但我认为权限不是问题,因为此证书已经存在于我自己用户帐户的个人存储中,并且我使用访问它的相同用户帐户生成了证书。