简体   繁体   中英

Azure SDK C# - Get Custom Domain Verification ID

I need to get the "Custom Domain Verification ID" from the azure sdk. I've been looking for several hours through the azure sdk documentation, but could not find it.

Its needed for an api to set custom domains programmatically and serve the correct information to set the 3rd party dns entries.

The Custom Domain Verification ID seems to be new to azure. Did anyone figured out how to get it from the sdk?

If you want to get Custom Domain Verification ID when you add custom domain name for Azure app service, we can use Resource Graph to query it.

For example

  1. create a service principal and assign Azure RABC role to the sp(I use 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"

在此处输入图像描述

  1. Code

    a. Install SDK

     Install-Package Microsoft.Identity.Client Install-Package Microsoft.Azure.Management.ResourceGraph

    b. Code

     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]); } }

Thanks Jim. Your answer mostly worked, but I had a few small issues with it. I think they might have changed some stuff in the library a bit. I refactored, and I'm using the following NuGet packages:

azure.identity v1.4.0
microsoft.azure.management.fluent v1.37.1
microsoft.azure.management.resourcegraph v2.1.0

And here is my code solution:

    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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM