Azure SDK C# - 获取自定义域验证 ID
Azure SDK C# - Get Custom Domain Verification ID
我需要从 azure sdk 获取 "Custom Domain Verification ID"。
我一直在通过 azure sdk 文档寻找几个小时,但找不到它。
api 需要它以编程方式设置自定义域并提供正确的信息以设置第 3 方 dns 条目。
自定义域验证 ID 似乎是 azure 的新功能。
有人知道如何从 sdk 获取它吗?
如果您想在为Azure应用服务添加自定义域名时获取自定义域验证ID,我们可以使用Resource Graph查询。
例如
- 创建服务主体并将 Azure RABC 角色分配给 sp(我使用 Azure CLI)
az login
#it will create a service principal and assign contributor role to the sp
az ad sp create-for-rbac -n "jonsp2"
代码
一个。安装 SDK
Install-Package Microsoft.Identity.Client
Install-Package Microsoft.Azure.Management.ResourceGraph
b。代码
string tenantId = "<>";
string clientId = "<>";
string authory = "https://login.microsoftonline.com/" + tenantId;
string clientSecret = "<>";
string subscriptionId = "<>";
var app = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(new Uri(authority))
.Build();
string[] scopes = { "https://management.azure.com/.default" };
var result = await app.AcquireTokenForClient(scopes)
.ExecuteAsync();
TokenCredentials tokenCredentials = new TokenCredentials(result.AccessToken,"Bearer");
var resourceGraphClient = new ResourceGraphClient(tokenCredentials);
var queryReq = new QueryRequest {
Subscriptions = new List<string> { subscriptionId },
Query = "project name, properties.customDomainVerificationId, type | where type == 'microsoft.web/sites' | order by name asc"
};
var result1 = await resourceGraphClient.ResourcesAsync(queryReq);
var r= (result1.Data as JObject).ToObject<Table>();
Console.WriteLine(r.Columns[0].Name + "\t" + r.Columns[1].Name + "\t" + r.Columns[2].Name);
foreach (var row in r.Rows) {
for (int i = 0; i < row.Count; i++) {
Console.WriteLine(r.Columns[i].Name + ": " + row[i]);
}
}
谢谢吉姆。您的回答大部分都有效,但我遇到了一些小问题。我想他们可能对图书馆里的一些东西做了一些改动。我进行了重构,并且正在使用以下 NuGet 包:
azure.identity v1.4.0
microsoft.azure.management.fluent v1.37.1
microsoft.azure.management.resourcegraph v2.1.0
这是我的代码解决方案:
private static AzureCredentials GetAzureCredentials()
{
return SdkContext.AzureCredentialsFactory.FromServicePrincipal(
Config.ClientId.ToString(),
Config.ClientSecret,
Config.TenantId.ToString(),
AzureEnvironment.AzureGlobalCloud);
}
private static async Task<string> GetCustomDomainVerificationId(string webAppName)
{
var kql = "Resources"
+ " | where type == 'microsoft.web/sites' and name == '" + webAppName + "'"
+ " | project properties.customDomainVerificationId";
return await QueryScalar(GetAzureCredentials(), kql);
}
private static async Task<string> QueryScalar(ServiceClientCredentials credentials, string kql)
{
var resourceGraphClient = new ResourceGraphClient(credentials);
var formatAsTable = new QueryRequestOptions {ResultFormat = ResultFormat.Table};
var queryRequest = new QueryRequest
{
Query = kql,
Options = formatAsTable
};
var result = await resourceGraphClient.ResourcesAsync(queryRequest);
var table = ((JObject) result.Data).ToObject<Table>();
var row = table.Rows.SingleOrDefault();
return row?[0].ToString();
}
我需要从 azure sdk 获取 "Custom Domain Verification ID"。 我一直在通过 azure sdk 文档寻找几个小时,但找不到它。
api 需要它以编程方式设置自定义域并提供正确的信息以设置第 3 方 dns 条目。
自定义域验证 ID 似乎是 azure 的新功能。 有人知道如何从 sdk 获取它吗?
如果您想在为Azure应用服务添加自定义域名时获取自定义域验证ID,我们可以使用Resource Graph查询。
例如
- 创建服务主体并将 Azure RABC 角色分配给 sp(我使用 Azure CLI)
az login
#it will create a service principal and assign contributor role to the sp
az ad sp create-for-rbac -n "jonsp2"
代码
一个。安装 SDK
Install-Package Microsoft.Identity.Client Install-Package Microsoft.Azure.Management.ResourceGraph
b。代码
string tenantId = "<>"; string clientId = "<>"; string authory = "https://login.microsoftonline.com/" + tenantId; string clientSecret = "<>"; string subscriptionId = "<>"; var app = ConfidentialClientApplicationBuilder.Create(clientId) .WithClientSecret(clientSecret) .WithAuthority(new Uri(authority)) .Build(); string[] scopes = { "https://management.azure.com/.default" }; var result = await app.AcquireTokenForClient(scopes) .ExecuteAsync(); TokenCredentials tokenCredentials = new TokenCredentials(result.AccessToken,"Bearer"); var resourceGraphClient = new ResourceGraphClient(tokenCredentials); var queryReq = new QueryRequest { Subscriptions = new List<string> { subscriptionId }, Query = "project name, properties.customDomainVerificationId, type | where type == 'microsoft.web/sites' | order by name asc" }; var result1 = await resourceGraphClient.ResourcesAsync(queryReq); var r= (result1.Data as JObject).ToObject<Table>(); Console.WriteLine(r.Columns[0].Name + "\t" + r.Columns[1].Name + "\t" + r.Columns[2].Name); foreach (var row in r.Rows) { for (int i = 0; i < row.Count; i++) { Console.WriteLine(r.Columns[i].Name + ": " + row[i]); } }
谢谢吉姆。您的回答大部分都有效,但我遇到了一些小问题。我想他们可能对图书馆里的一些东西做了一些改动。我进行了重构,并且正在使用以下 NuGet 包:
azure.identity v1.4.0
microsoft.azure.management.fluent v1.37.1
microsoft.azure.management.resourcegraph v2.1.0
这是我的代码解决方案:
private static AzureCredentials GetAzureCredentials()
{
return SdkContext.AzureCredentialsFactory.FromServicePrincipal(
Config.ClientId.ToString(),
Config.ClientSecret,
Config.TenantId.ToString(),
AzureEnvironment.AzureGlobalCloud);
}
private static async Task<string> GetCustomDomainVerificationId(string webAppName)
{
var kql = "Resources"
+ " | where type == 'microsoft.web/sites' and name == '" + webAppName + "'"
+ " | project properties.customDomainVerificationId";
return await QueryScalar(GetAzureCredentials(), kql);
}
private static async Task<string> QueryScalar(ServiceClientCredentials credentials, string kql)
{
var resourceGraphClient = new ResourceGraphClient(credentials);
var formatAsTable = new QueryRequestOptions {ResultFormat = ResultFormat.Table};
var queryRequest = new QueryRequest
{
Query = kql,
Options = formatAsTable
};
var result = await resourceGraphClient.ResourcesAsync(queryRequest);
var table = ((JObject) result.Data).ToObject<Table>();
var row = table.Rows.SingleOrDefault();
return row?[0].ToString();
}