获取Azure Cloud Service中的x509证书

时间:2016-05-04 13:07:51

标签: c# .net azure x509 azure-cloud-services

我需要使用证书对Azure Key Vault进行身份验证,但我无法访问我上传的密钥。我采取了以下措施:

通过门户网站将密钥(.pfx)上传到Cloud Service。

将此添加到ServiceConfiguration

<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

我还需要其他配置吗?

1 个答案:

答案 0 :(得分:2)

您收到此错误的原因是您要求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);
相关问题