azure - Import-AzKeyVaultCertificate cmdlet 导致:密钥对处于指定状态的用户无效
问题描述
我正在尝试将 .pfx 证书导入 Azure 密钥库,但遇到了一些问题。
Import-AzKeyVaultCertificate -VaultName "SecHash03" -Name "CodeSigning" -FilePath "\path\to\my\cert.pfx"
结果是:
Import-AzKeyVaultCertificate : Key not valid for use in specified state.
At line:1 char:1
+ Import-AzKeyVaultCertificate -VaultName SecHash03 -Name " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Import-AzKeyVaultCertificate], CryptographicException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.KeyVault.ImportAzureKeyVaultCertificate
我正在使用 certreq 从与 CA 位于同一域的机器中向企业 CA 请求此证书。导入证书不需要密码。然后计划将该证书上传到上述 Azure 密钥库。
我尝试使用 Azure 门户导入此证书,效果很好;导入和使用都很好。因此,正如另一个类似的 Stackoverflow 答案( Importing certificate to Azure Key Vault: Key not valid for use in specified state)中所建议的那样,这不是角色的问题。
请指教!
解决方案
据我所知,当您将预先存在的 .pfx 文件证书导入 Azure 密钥保管库时,您需要提供用于保护证书的密码,因为您需要在私钥中导出证书并包含所有证书如果可能,在证书路径中。例如,
# Export the cert to a PFX with password
$password = ConvertTo-SecureString "Password!" -AsPlainText -Force
Export-PfxCertificate -Cert "cert:\CurrentUser\My\$($cert.Thumbprint)" -FilePath C:\temp\cert2.pfx -Password $password
# Upload to Key Vault
Import-AzureKeyVaultCertificate -VaultName noel-temp -Name cert2 -FilePath C:\temp\cert2.pfx -Password $password
或者,
如果您使用受支持的 CA,您甚至可以将 Key Vault 配置为代表您注册证书。不泄露密钥!为简单起见,这些示例中的策略将设置为从 Key Vault 生成自签名证书。
# Have Key Vault create the certificate with a simple policy
$policy = New-AzureKeyVaultCertificatePolicy -SubjectName "CN=mycluster.southcentralus.cloudapp.azure.com" -IssuerName Self -ValidityInMonths 12
Add-AzureKeyVaultCertificate -VaultName noel-temp -Name cert1 -CertificatePolicy $policy
# Download the secret (private key information) associated with the cert
$secret = Get-AzureKeyVaultSecret -VaultName noel-temp -Name cert1
$secretBytes = [System.Convert]::FromBase64String($secret.SecretValueText)
[System.IO.File]::WriteAllBytes("C:\temp\cert1.pfx", $secretBytes)
# Import the certificate to CurrentUser\My
Import-PfxCertificate -FilePath C:\temp\cert1.pfx -CertStoreLocation cert:\CurrentUser\My -Exportable
您可以从这两个链接中获得更多详细信息:
推荐阅读
- node.js - Node.js 强大 :: 如何使用(php 约定)数组字段解析表单“multipart/form-data”
- android - android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class Button
- reactjs - 如何模拟从 Jest / 酶测试中的下拉列表中选择?
- ms-access - MS-Access VBA:将变量从一种形式传递到另一种形式,导致运行时错误
- c# - c#如何从另一个类调用一个类中的接口方法?
- java - 带有本机 Android 应用程序的 Azure SignalR 服务?
- javascript - 如何在没有状态的情况下在 React 中填充 DropdownList 选项?
- sql - 如何使用均值、中位数和众数创建新的 SQL 表?
- python-3.x - Python:反斜杠和正号是什么意思?
- python - 如何在 Outlook 和 Python 中从辅助帐户发送电子邮件