'c# - Get TLS/SSL certificate from Azure App Serivce

I need help.

I have the following code in C# and I need to get the expiration date of the TLS certificate of an App Serivce.

I am obtaining the token without any problem, but when obtaining the list of certificates, it is coming back empty. It is the first time that I work with this API and I don't know if I have to establish any permissions in the client in Azure (the one I use to obtain the token).

Does anyone know why I don't get data? Thanks for any help you can provide me.

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;

namespace AzureInformation
{
    class Program
    {
        public static string tenantId = "";
        public static string subscriptionId = "";

        public static string clientId = "";
        public static string secret = "";

        static void Main(string[] args)
        {
            Token token = GetToken();

            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.token_type, token.access_token);

            string certificatesUrl = $"https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Web/certificates?api-version=2021-02-01";
            var response = client.GetAsync(certificatesUrl).Result;
            response.EnsureSuccessStatusCode();
            
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            
            Console.ReadKey();
        }

        public static Token GetToken()
        {
            var resourceUrl = "https://management.azure.com/";
            var requestUrl = $"https://login.microsoftonline.com/{tenantId}/oauth2/token";

            var httpClient = new HttpClient();
            var dict = new Dictionary<string, string>
            {
                { "grant_type", "client_credentials" },
                { "client_id", clientId },
                { "client_secret", secret },
                { "resource", resourceUrl }
            };

            var requestBody = new FormUrlEncodedContent(dict);
            var response = httpClient.PostAsync(requestUrl, requestBody).Result;
            response.EnsureSuccessStatusCode();
            var responseContent = response.Content.ReadAsStringAsync().Result;
            var aadToken = JsonSerializer.Deserialize<Token>(responseContent);
            return aadToken;
        }
    }

    public class Token
    {
        public string token_type { get; set; }
        public string access_token { get; set; }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source