Powershell "X509Certificate2Collection"异常调用 "Import"和 "3"参数 : "Cannot find the requested object

标签 powershell x509certificate x509 x509certificate2 azure-keyvault

我有以下代码可以从 Azure Key Vault 下载证书。

   $secretName = "TestCert"
    $kvSecret = Get-AzureKeyVaultSecret -VaultName $vaultName -Name $certificateName
    $kvSecretBytes = [System.Convert]::FromBase64String($kvSecret.SecretValueText)
    $certCollection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
    $certCollection.Import($kvSecretBytes,$null, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)

但是在将证书导入 certCollection 时,导入方法抛出以下错误。
Exception calling "Import" with "3" argument(s): "Cannot find the requested object.
"
At C:\Users\abc\Desktop\test2.ps1:8 char:1
+ $certCollection.Import($kvSecretBytes,$null,[System.Security.Cryptogr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CryptographicException     

非常感谢这方面的帮助。谢谢

最佳答案

像这样更改代码,您就可以开始了!

    $secretName = "TestCert"
    $kvSecret = Get-AzureKeyVaultSecret -VaultName $vaultName -Name $certificateName
    $kvSecretBytes = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($kvSecret.SecretValueText))
    $jsonCert = ConvertFrom-Json($kvSecretBytes)
    $certBytes = [System.Convert]::FromBase64String($jsonCert.data)
    $certCollection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
    $certCollection.Import($certBytes,$jsonCert.password,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)

关于Powershell "X509Certificate2Collection"异常调用 "Import"和 "3"参数 : "Cannot find the requested object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45089108/

相关文章:

regex - 从字符串中提取文本

curl: (60) SSL 证书问题:无法获取本地颁发者证书

encryption - RSA解密抛出 'Access denied'异常

ssl - CA 认证签名过程中 RDN 属性的排序

json - 调用 REST api 并将生成的 json 数据插入 SQL Server 的最简单方法

powershell - 使用Write-Error在Powershell中写入错误流

c - 如何从 X509 证书中获取 key 使用值?

c# - WCF 证书链,以编程方式验证

powershell - 是否可以使用 Powershell 命令 get-counter 获取性能计数器描述?

wcf - 通过 HTTPS 上的客户端证书对 WCF 请求进行身份验证