unable to list all the function apps and their trigger keys programmatically in .net core

Tiwari, Vivek K 0 Reputation points
2024-09-09T20:19:27.1166667+00:00

unable to list all the function apps and their trigger keys programmatically in .net core. All the available packages are deprecated hence i cant use them.

How can I programmatically list the function app http trigger names and their trigger keys in .Net Core and retrieve their information

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,913 questions
{count} votes

1 answer

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 14,831 Reputation points Microsoft Employee
    2024-09-10T00:05:09.6933333+00:00

    Hi @Tiwari, Vivek K Greetings! Welcome to Microsoft Q&A forum. Thank you for posting this question here.

    As you stated in the question, there are no libraries available to extract this information directly. The documentation on Obtaining the function access keys provides couple of approaches to achieve this. I have tested the following .Net code to extract the function name and the keys for it under a function app by calling the Azure CLI commands from the code

    Updated code using the API endpoint

    using System;
    using System.Diagnostics;
    using Azure.Identity;
    using Azure.Core;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    
    
    namespace ConsoleApp
    {
        internal class Program
        {
            static async Task Main(string[] args)
            {
                // Replace these values with your own
                // Replace these values with your own            
                string resourceGroupName = "<resourcegroupname>";
                string functionAppName = "<functionappname>";
    
                // Create a new instance of DefaultAzureCredential
                var credential = new DefaultAzureCredential();
                //Read using the API
                string subscriptionId = "<subscriptionID>";
                string apiVersion = "2023-12-01";
    
                var token = await credential.GetTokenAsync(new Azure.Core.TokenRequestContext(new[] { "https://management.azure.com/.default" }));
    
                var client = new HttpClient();
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token.Token);
    
                //End point to list all functions under a function app
                var url = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{functionAppName}/functions?api-version={apiVersion}";
    
                var response = await client.GetAsync(url);
                var content = await response.Content.ReadAsStringAsync();
    
                var jsonDoc = JObject.Parse(content);
    
                JToken apiResult = jsonDoc["value"];
    
                //End point to obtain the Master Key of the hosting function app
                url = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{functionAppName}/host/default/listkeys?api-version={apiVersion}";
    
                response = await client.PostAsync(url, null);
                var masterKeys = await response.Content.ReadAsStringAsync();
                jsonDoc = JObject.Parse(masterKeys);
                JToken funcappKey = jsonDoc["masterKey"];
                Console.WriteLine($"Funcation app master key is {funcappKey.ToString()}");
    
                //var masterKey = functionAppKeys.MasterKey;
    
                //Console.WriteLine(masterKey);
    
                foreach (var funcs in apiResult)
                {
                    var config = funcs["properties"]["config"];
                    string funcname = (config["name"]).ToString();
                    string bindingtype = config["bindings"][0]["type"].ToString();
                    Console.WriteLine("Function name is " + funcname + " and trigger type is " + bindingtype);
                    //End point to return the function keys
                    url = $"https://{functionAppName}.azurewebsites.net/admin/functions/{funcname}/keys";
                    client.DefaultRequestHeaders.Add("x-functions-key", funcappKey.ToString());
                    response = await client.GetAsync(url);
                    content = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(content);
                }
            }
        }
    }
    
    

    The above code extracts the function name and the type of the Trigger associated with it from the returned result set and uses the name to extract the keys. Below is the sample output from the above code

    enter image description here

    Hoper this helps! Please let us know if you have any additional questions or need further assitance.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.