【问题标题】:azure sdk link certificate into new resource groupazure sdk 将证书链接到新资源组
【发布时间】:2019-10-02 09:59:19
【问题描述】:

我正在从 ARM 模板迁移到 C# Azure SDK 部署选项,并且在将现有证书的使用转换为新资源组时遇到问题。 我将 SSL 证书存储到特定的资源组和 Keyvault 中。 我有 3 个环境到 3 个使用此证书(webapps ssl 绑定)的 ARM 模板创建的其他资源组中。 当我从 azure 门户进入我的证书时,我可以看到私有证书链接到我的 3 个资源组

这块 JSON 将 ssl 证书导入到新的资源组中

{
      "type": "Microsoft.Web/certificates",
      "name": "MyCert",
      "apiVersion": "2016-03-01",
      "location": "[variables('Location')]",
      "properties": {
        "keyVaultId": "[parameters('keyvaultId')]",
        "keyVaultSecretName": "[parameters('KeyvaultSecretName')]",
        "thumbprint": "[parameters('certThumbprint')]"
      },
      "dependsOn": [
        "[concat('Microsoft.Web/serverfarms/', variables('WebAppAppPlanName'))]"
      ],
    },

使用这块 json 通过其指纹创建 ssl 绑定:

{
      "name": "[concat(variables('WebAppName'),'/',variables('subDomain'), '.domain.fr')]",
      "apiVersion": "2016-08-01",
      "type": "Microsoft.Web/sites/hostNameBindings",
      "location": "[variables('Location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Web/sites', variables('WebAppName'))]"
      ],
      "tags": {
        "displayName": "HostName"
      },
      "properties": {
        "siteName": "[variables('WebAppName')]",
        "customHostNameDnsRecordType": "CName",
        "hostNameType": "Verified",
        "sslState": "SniEnabled",
        "thumbprint": "[reference(resourceId('Microsoft.Web/certificates', 'CertName')).Thumbprint]"
      }
    },

现在我正在尝试使用 Azure SDK 创建相同的东西:

 webApp = webApp.Update()
                  .DefineHostnameBinding()
                  .WithThirdPartyDomain(domain)
                  .WithSubDomain(subdomain)
                  .WithDnsRecordType(CustomHostNameDnsRecordType.CName)
                  .Attach()
                  .Apply();

            webApp = webApp.Update()
                        .WithThirdPartyHostnameBinding(domain, subdomain)
                        .DefineSslBinding()
                            .ForHostname(hostName)
                            .WithExistingCertificate(certificateThumbPrint)
                            .WithSniBasedSsl()
                            .Attach()
                        .Apply();

此代码不起作用,因为在我新创建的资源组中找不到 certificateThumbprint。 缺少的是将我的证书链接到我的资源组的第一个 json 块的等价物。 使用 azure 门户并手动执行此操作称为“导入应用服务证书”

如何使用 C# 中的 azure SDK 以编程方式将我现有的应用服务证书导入我的新资源组?

【问题讨论】:

  • 我认为您需要先创建证书。看看BeginCreateOrUpdateCertificate的方法。
  • 我已将证书放入我的密钥库。在创建 ssl 绑定之前,我需要通过将此应用证书导入我的新资源组来重用它(最后一个代码块有两个更新)
  • 看看吧。该方法意味着创建一个类型为 Microsoft.Web/certificates 的资源。
  • 我检查了您的链接,但我并不真正了解此功能的真正目的。它会创建一个新证书吗?它只创建一个“链接”吗?没有如何实施它的例子。它只说“创建或更新证书并与密钥保管库机密相关联。”。你能补充一点解释吗?我尝试搜索此方法的一些文档、示例、用例,但没有结果
  • 我认为在我的新资源组中创建 appcertificate 的方法在这里:var certificate = azure.AppServices.AppServiceCertificates .Define($"CertName") .WithRegion(ResourcesHelper.Location) .WithExistingResourceGroup(RG) .WithPfxByteArray(pfx) .WithPfxPassword(pwd) .Create(); 现在的困难是从我的密钥库中获取 PFX 和密码......我尝试了很多东西,但没有任何效果

标签: c# azure ssl sdk certificate


【解决方案1】:

当尝试将现有证书从密钥保管库导入应用服务时,您需要先将密钥保管库证书添加/链接到资源组。

To do this with Azure REST API

使用旧的 Azure SDK (Microsoft.Azure.Management.WebSites) 时,有一个可用的类 BeginCreateOrUpdateCertificate 来执行此操作。但微软不推荐使用它,因为它可能很快就会被弃用。

使用 Azure fluent SDK - 我们无法使用默认的应用程序证书管理类来实现这一点。 “Microsoft.Azure.Management.Fluent.Azure.AppServices.AppServiceCertificates.Define()”。

但是,“Microsoft.Azure.Management.ResourceManager.Fluent.Core”命名空间中有一个名为 RestClient 的类,旨在手动调用 Azure API。我们可以使用WebSiteManagementClient,它使用 RestClient 对 Web 应用资源进行 REST API 调用。

var restClient = Microsoft.Azure.Management.ResourceManager.Fluent.Core.RestClient.Configure().WithEnvironment(AzureEnvironment.AzureGlobalCloud).WithCredentials(credentials).Build();
        
        
var x = new WebSiteManagementClient(restClient);
x.SubscriptionId = "your-subscription-id";
var result = x.Certificates.CreateOrUpdateAsync(resourceGroupName, "UploadTest", new CertificateInner("your certificate-location (East-US)",
                        "certificate-password (pass string.Empty if null)",                            
                        keyVaultId: "/subscriptions/your-subscription-id/resourcegroups/your-resourcegroup-id/providers/microsoft.keyvault/vaults/your-key-vault-id",
                        keyVaultSecretName: "key-vault-secretname", hostNames: new List<string>() { "hostname" })).GetAwaiter().GetResult();

这会将密钥保管库中的现有证书添加/链接到资源组,并且可供应用服务使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 2022-08-17
    相关资源
    最近更新 更多