在 Azure 云服务中获取 x509 证书

Get x509 Certificate In Azure Cloud Service

我需要使用证书对 Azure Key Vault 进行身份验证,但我无法访问我上传的密钥。我已经采取了这些步骤:

已通过门户将密钥 (.pfx) 上传到云服务。

将此添加到服务配置

<Certificates>
    <Certificate name="keyvault" thumbprint="<my_thumbprint>" thumbprintAlgorithm="sha1" />
</Certificates>

将此添加到 ServiceDefinition

<Certificates>
  <Certificate name="keyvault" storeLocation="LocalMachine" storeName="CA" />
</Certificates>    

使用此代码检索密钥:

var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
StoreLocation.LocalMachine);
try
{
    store.Open(OpenFlags.ReadOnly);
    var col = store.Certificates.Find(X509FindType.FindByThumbprint,
                <thumbprint_value>, false); // Don't validate certs, since the test root isn't installed.
    if (col == null || col.Count == 0)
                return null;
            return col[0];
}
finally
{
    store.Close();
}

但是,当我启动服务时,我看到了这个异常:

Value cannot be null.
Parameter name: certificate

我需要任何额外的配置吗?

您收到此错误的原因是您要求 Fabric Controller 在一个位置安装证书

<Certificate name="keyvault" storeLocation="LocalMachine" storeName="CA" />

当您的代码正在从其他位置读取证书时。

var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

请确保您在两个地方使用相同的位置。

我将在 csdef 文件中进行以下更改:

<Certificate name="keyvault" storeLocation="LocalMachine" storeName="My" />

以及代码中的以下内容:

var store = new X509Store(StoreName.CertificateAuthority, StoreLocation.LocalMachine);