【发布时间】:2013-04-17 01:24:18
【问题描述】:
我使用以下 PowerShell 函数将 PFX 导入到我的 Windows 2008 R2 服务器的证书存储中
function Import-PfxCertificate ([String]$certPath,[String]$certificateStoreLocation = "CurrentUser",[String]$certificateStoreName = "My",$pfxPassword = $null)
{
$pfx = new-object System.Security.Cryptography.X509Certificates.X509Certificate2
$pfx.Import($certPath, $pfxPassword, "Exportable,PersistKeySet")
$store = new-object System.Security.Cryptography.X509Certificates.X509Store($certificateStoreName,$certificateStoreLocation)
$store.open("MaxAllowed")
$store.add($pfx)
$store.close()
return $pfx
}
函数的调用者看起来像$importedPfxCert = Import-PfxCertificate $pfxFile "LocalMachine" "My" $password 我将它安装到本地机器的我的商店。然后我授予对我的 IIS 应用程序池的读取权限。
我有一个 WCF 服务需要使用它
<behaviors>
<serviceBehaviors>
<behavior>
<serviceCredentials>
<serviceCertificate findValue="MyCertName" x509FindType="FindBySubjectName" />
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="MyValidator" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
当我使用客户端调用服务时,我收到了来自 WCF It is likely that certificate 'CN=MyCertName' may not have a private key that is capable of key exchange or the process may not have access rights for the private key. 的异常
如果我将其从 MMC 中删除,并从证书 MMC 手动导入相同的 PFX 文件到相同的存储并授予相同的权限,我的客户可以毫无问题地调用该服务。
所以这让我想到,出于某种原因,如果我使用 PowerShell,私钥会以某种方式被搞砸。
有趣的是,无论哪种方式,我都去 MMC 并双击我安装的证书,我可以看到You have a private key that corresponds to the certificate.,所以看起来即使在 PowerShell 中也加载了私钥。权限设置相同。
有什么线索或经验吗?
【问题讨论】:
标签: powershell-2.0 x509certificate