Search code examples
c#azureazure-appservice

How to execute the POST List Application Settings for Azure App Service


Via Azure App Service, I want to get the application settings of an app. https://learn.microsoft.com/en-us/rest/api/appservice/web-apps/list-application-settings.

When I'm using the "Try-it" from the site and supplying the correct parameters, the call works. However, in my VS project it is not working.

Below I've created a simple HTTPClient to do so, but it is not working. What am I missing?

        string clientId = "aaa";
        string clientSecret = "bbb";
        string tenantId = "ccc";

        var subscriptionId = "ddd";
        var resourceGroupName = "eee";
        var name = "fff";

        string apiUrl =
$"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/config/appsettings/list?api-version=2022-03-01";

        try
        {
            IConfidentialClientApplication app = ConfidentialClientApplicationBuilder
                .Create(clientId)
                .WithClientSecret(clientSecret)
                .WithAuthority(new Uri($"https://login.microsoftonline.com/{tenantId}"))
                .Build();

            var scopes = new string[] { "https://management.azure.com/.default" };

            var result = await app.AcquireTokenForClient(scopes)
                .ExecuteAsync();

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var content = new StringContent(String.Empty, Encoding.UTF8, "application/json");

                HttpResponseMessage response = await httpClient.PostAsync(apiUrl, content);

                if (response.IsSuccessStatusCode)
                {
                    string responseContent = await response.Content.ReadAsStringAsync();

                    Console.WriteLine("API Response:");
                    Console.WriteLine(responseContent);
                }
                else
                {
                    Console.WriteLine($"API call failed with status code: {response.StatusCode}");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }

I'm guessing I had something to do with the PostAsync method.

Thanks in advance!

I have tried to do a GET-request (https://learn.microsoft.com/en-us/rest/api/appservice/web-apps/get-function) and that works.

SOLVED, see Ikhtesam Afrin's comment. Added the role "Website Contributor" to the Azure AD registered app inside function app.


Solution

  • I have used your code and got the expected result as shown in the below screenshot.

    enter image description here

    I only made the below changes in my function app. Please add the website contributor role to your Azure AD registered app inside function app.

    enter image description here

    Output:

    enter image description here .