Skip to main content
 首页 » 编程设计

c#之如何从 C#之Web 应用程序向 Azure 资源添加标签

2024年02月05日30sharpest

如何在我的 ASP.NET 应用程序中使用 C# 代码将标签添加到 azure 资源。 我正在尝试创建一个 Azure 标签管理门户。

我找到了这个 question但它是关于向资源组添加标签。此外,该图书馆似乎已被弃用。如果有人知道如何将标签附加到资源,请帮忙。

注意:(i)我试过 Azure ServiceManagement API但我看到没有 API 支持将标签附加到资源。 (ii) 如果没有别的办法,powershell Cmdlets 是否是一个可行的选择?

请您参考如下方法:

点击此链接在 AD 和服务原则中创建客户端应用程序。记下tenantId、ClientId和ClientKey

https://learn.microsoft.com/en-us/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);