簡體   English   中英

如何從C#向Web資源添加標簽-Web應用程序

[英]How to Add Tags to Azure Resource from a C# - Web Application

如何在ASP.NET應用程序中使用C#代碼將標簽添加到蔚藍資源中。 我正在嘗試創建一個天藍色標簽管理門戶。

我發現了這個問題,但這是關於將標簽添加到資源組。 另外,圖書館似乎已被棄用。 如果有人知道如何將標簽附加到資源,請提供幫助。

注意:(i)我已經嘗試過Azure ServiceManagement API,但是我看不到將標簽附加到資源的API支持。 (ii)如果沒有其他方法,powershell Cmdlet是否將是可行的選擇?

單擊此鏈接以AD和服務原理創建客戶端應用程序。 記下tenantId,ClientId和ClientKey

https://docs.microsoft.com/zh-CN/azure/azure-resource-manager/resource-group-create-service-principal-portal

查找資源,添加/更新標簽並打補丁。

var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, clientKey);
var resourceClient = new ResourceManagementClient(serviceCreds);
resourceClient.SubscriptionId = subscriptionId;

GenericResource genericResource = resourceClient.Resources.Get("some-resource-group", "Microsoft.DocumentDB", "", "databaseAccounts", "some-resource", "2016-03-31");

genericResource.Tags.Add("Version", "1.0");

resourceClient.Resources.CreateOrUpdate("some-resource-group", "Microsoft.DocumentDB", "", "databaseAccounts", "some-resource", "2016-03-31", genericResource);

PowerShell可以輕松地做到這一點,您可以使用以下命令:

PS C:\> Set-AzureRmResource -Tag @( @{ Name="tag_name"; Value="tag_value" }) -ResourceId <resource_id>

查看本文以了解詳細信息。

如果有人對嘗試通過REST API進行嘗試感興趣,那么在使用Fiddler監視Powershell綁定流量后,我就發現了這一點。 注意標記為json-payload。

https://management.azure.com/subscriptions/ {subscription-id} / resourceGroups / {resource-group-name} /providers/Microsoft.Compute/virtualMachines/ {VM-Name}

PATCH **https://management.azure.com/subscriptions/6dcd{subscrID}e8f/resourceGroups/css-dev/providers/Microsoft.Sql/servers/css-development/databases/css-dev?api-version=2014-04-01 HTTP/1.1**
Authorization: Bearer sDSEsiJKV1QiLCJhbG[<PARTIALLY REMOVED BY KIRAN FOR SECURITY REASON>]XDvZBJG5Jhh0rivehvDS
User-Agent: AzurePowershell/v1.0.0.0
ParameterSetName: Resource that resides at the subscription level.
CommandName: Set-AzureRmResource
Content-Type: application/json; charset=utf-8
Host: management.azure.com
Content-Length: 52
Expect: 100-continue
Connection: Keep-Alive

**{
  "tags": {
    "displayName": "AzureV1"
  }
}**

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM