【问题标题】:Get x509 Certificate In Azure Cloud Service在 Azure 云服务中获取 x509 证书
【发布时间】:2016-05-04 13:07:50
【问题描述】:

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

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

将此添加到 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

我还需要什么额外的配置吗?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 谢谢。使用此配置在本地工作,使用 certmgr 安装证书,但在部署到云服务时不能。
    • 这可能是因为您可能已经将证书安装在您正在本地计算机上阅读的商店中。请尝试通过从“CurrentUser”中的“Personal”证书存储中删除证书来运行您的代码。
    • 我将证书本地保存在与上传到 Azure 后保存的证书不同的存储中的问题。为了让它在 Azure 中工作,我需要像这样创建 x509 存储:var store = new X509Store(StoreName.CertificateAuthority, StoreLocation.LocalMachine)。如果您更新最后两行代码,我可以将您的答案标记为正确
    • 没错。您在代码中读取证书的位置必须与 csdef 文件中定义的内容匹配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 2018-11-24
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多