How to access a certifciate with error: "Unable to retrieve certificates because the thumbprint is not valid"

How to access a certifciate with error: "Unable to retrieve certificates because the thumbprint is not valid"

我遇到了错误

Unable to retrieve certificates because the thumbprint is not valid. Verify the thumbprint and retry.

当我尝试使用 LocalMachine 证书存储中的证书时。

我有一个管理员帐户在 LocalMachine 证书存储中安装证书(包括私钥),并为某些用户(例如功能 ID)。

我希望能够 运行 以下代码获取指纹,然后在 Invoke-WebRequest 调用中使用:

$certStorePath  = "Cert:\LocalMachine\My"
$certDetails    = Get-ChildItem -Path $certStorePath | Where-Object {$_.Subject -like "*myCert*"}   # Returns one result
$certThumbprint = $certDetails.Thumbprint

Invoke-WebRequest -Uri $externalUrl -Proxy $proxyServer -UseBasicParsing -CertificateThumbprint $certThumbprint

我可以获得证书详细信息,包括指纹($certDetails),但似乎权限不允许我(或 FID)使用 证书(或者可能只是访问证书的私钥部分)。当证书安装在 CurrentUser 商店中时,该代码有效。

如何为此类非管理员用户启用对 LocalMachine 存储中的证书的访问权限?

问题似乎与 Invoke-WebRequest 及其在这段代码中的使用方式有关。

第一部分代码能够成功访问证书:

$certStorePath  = "Cert:\LocalMachine\My"
$certDetails    = Get-ChildItem -Path $certStorePath | Where-Object {$_.Subject -like "*myCert*"}

然而,尽管指纹在所有证书存储中都是唯一的(例如,这个指纹仅存在于 LocalMachine 中),Invoke-WebRequest 无法访问它,因为 只会查看 CurrentUser 证书库。

所以,总的来说要让它工作:

  1. 安装包含私钥的证书。
  2. 提供对证书私钥部分的访问权限,到适当的users/FID。
  3. 使用 Get-ChildItem 获取证书本身,并将 that 传递给 Invoke-WebRequest 而不是指纹:
$certStorePath = "Cert:\LocalMachine\My"
$certificate   = Get-ChildItem -Path $certStorePath | Where-Object {$_.Subject -like "*myCert*"}   # Returns one result

Invoke-WebRequest -Uri $externalUrl -Proxy $proxyServer -UseBasicParsing -Certificate $certificate