【问题标题】:Add PFX Certificate to Azure WebApp using ARM (not for ssl)使用 ARM 将 PFX 证书添加到 Azure WebApp(不适用于 ssl)
【发布时间】:2016-04-26 02:04:02
【问题描述】:

使用 Azure 资源管理器支持的 Rest Management APIS,以下代码将证书从 keyvault 添加到 ARM。

var secret = keyvaultClient.GetSecretAsync(vaultUri, options.CertificateName).GetAwaiter().GetResult();

var certUploaded = client.Certificates.CreateOrUpdateCertificateWithHttpMessagesAsync(
    options.ResourceGroupName, options.CertificateName,
    new Certificate {
        PfxBlob = secret.Value,
        Location = app.Body.Location
    }).GetAwaiter().GetResult();

var appSettings = client.Sites.ListSiteAppSettingsWithHttpMessagesAsync(options.ResourceGroupName, options.WebAppName).GetAwaiter().GetResult();
var existing = (appSettings.Body.Properties["WEBSITE_LOAD_CERTIFICATES"] ?? "").Split(',').ToList();
if (!existing.Contains(certUploaded.Body.Thumbprint))
    existing.Add(certUploaded.Body.Thumbprint);

appSettings.Body.Properties["WEBSITE_LOAD_CERTIFICATES"] = string.Join(",",existing);
appSettings.Body.Properties[$"CN_{options.CertificateName}"] = certUploaded.Body.Thumbprint;

var result = client.Sites.UpdateSiteAppSettingsWithHttpMessagesAsync(options.ResourceGroupName, options.WebAppName, appSettings.Body).GetAwaiter().GetResult();

问题是在webapp中加载它时

        X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        certStore.Open(OpenFlags.ReadOnly);
        X509Certificate2Collection certCollection = certStore.Certificates.Find(
                                   X509FindType.FindByThumbprint,
                             // Replace below with your cert's thumbprint
                             "0CE28C6246317AEB00B88C88934700865C71CBE0",
                                   false);

        Trace.TraceError($"{certCollection.Count}");
        Console.WriteLine($"{certCollection.Count}");
        // Get the first cert with the thumbprint
        if (certCollection.Count > 0)
        {
            X509Certificate2 cert = certCollection[0];
            // Use certificate
            Console.WriteLine(cert.FriendlyName);
        }
        certStore.Close();

它没有加载。

如果我改为使用门户上传它,一切都会按预期进行。

我还注意到,在门户中上传的证书在 ARM 中不存在,只有在帖子开头添加了代码的证书存在。:

那么我们需要做些什么来使证书可用于不涉及手动上传到门户的 webapp?

【问题讨论】:

  • 我最近遇到了类似的问题 - 请参阅 How to add a certificate to an Azure RM website with Powershell(我正在计算您的代码!)
  • 您是从其他地方获得的代码吗? appsettings 部分对我来说“感觉”不对(但我不确定如何/为什么 - 所以很可能完全错了)
  • 我只是一起破解了它。无论如何,您成功使用 Azure RM 网站 powersell 了吗?那我就看看他们是怎么做到的
  • 我可以看到它与添加 SSL 有关。我没有添加 SSL。我只需要应用程序的证书来解密一些值。
  • 我的答案主要来自资源资源管理器、资源模板和 Azure Powershell 的来源。如果您从资源资源管理器中配置的解决方案的外观开始,然后尝试使用模板的 sn-p 将其推送。我看不出添加 SSL 证书和普通证书之间存在巨大差异 - 但是我在上面写的答案非常有效。

标签: azure certificate azure-web-app-service


【解决方案1】:

问题在于证书应该添加到 webapp 所在的 serverfarm 的资源组中,而不是 webapp 的资源组中。

更改代码以部署到正确的资源组解决了所有问题。

作为参考,我的更新代码在这里:

var vaultUri = $"https://{options.VaultName}.vault.azure.net";
var keyvaultClient = new KeyVaultClient((_, b, c) => Task.FromResult(options.VaultAccessToken));

using (var client = new WebSiteManagementClient(
    new TokenCredentials(cred.AccessToken)))
{
    client.SubscriptionId = cred.SubscriptionId;

    var app = client.Sites.GetSite(options.ResourceGroupName, options.WebAppName);
    var serverFarmRG = Regex.Match(app.ServerFarmId, "resourceGroups/(.*?)/").Groups[1];

    var secret = keyvaultClient.GetSecretAsync(vaultUri, options.CertificateName).GetAwaiter().GetResult();

    var certUploaded = client.Certificates.CreateOrUpdateCertificate(
        serverFarmRG.Value, options.CertificateName,
        new Certificate
        {
            PfxBlob = secret.Value,
            Location = app.Location
        });

    var appSettings = client.Sites.ListSiteAppSettings(options.ResourceGroupName, options.WebAppName);
    appSettings.Properties["WEBSITE_LOAD_CERTIFICATES"] = string.Join(",", client.Certificates.GetCertificates(serverFarmRG.Value).Value.Select(k => k.Thumbprint));
    appSettings.Properties[$"CN_{options.CertificateName}"] = certUploaded.Thumbprint;

    var result = client.Sites.UpdateSiteAppSettings(options.ResourceGroupName, options.WebAppName, appSettings);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-18
    • 2020-01-18
    • 2016-12-14
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    相关资源
    最近更新 更多