[英]azure sdk link certificate into new resource group
我正在從 ARM 模板遷移到 C# Azure SDK 部署選項,並且在將現有證書的使用轉換為新資源組時遇到問題。 我有一個存儲在特定資源組和 Keyvault 中的 SSL 證書。 我將 3 個環境分為 3 個其他資源組,這些資源組是使用 ARM 模板創建的,這些資源組使用此證書(webapps ssl 綁定)。 當我從 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 以編程方式將我現有的應用服務證書導入到我的新資源組中?
嘗試將現有證書從 Key Vault 導入應用服務時,您需要先將 Key Vault 證書添加/鏈接到資源組。
使用舊的 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。 我們可以利用使用RestClient的 WebSiteManagementClient 對 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();
這會將 Key Vault 中的現有證書添加/鏈接到資源組,並且可供應用服務使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.