Search code examples
c#azurednssdkverification

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?


Solution

  • 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"
    

    enter image description here

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