question

Juanjo-9227 avatar image
0 Votes"
Juanjo-9227 asked

Authentication error while trying to display a User's subscription list

What I want is to Log in with a Microsoft Azure account and, then, obtain my Azure subscriptions. To do that, I need to add an authentication header to the HTTP GET request.


  static async Task GetATokenForGraph()
     {
         PublicClientApp = PublicClientApplicationBuilder.Create(clientId)
                 .WithRedirectUri("http://localhost")
                 .WithAuthority(AzureCloudInstance.AzurePublic, tenantId)
                 .Build();

         var _scopes = new string[] { "user.read.all" }.AsEnumerable();
         var authResult = await PublicClientApp.AcquireTokenInteractive(_scopes)
                                     .ExecuteAsync();

         var httpClient = new HttpClient();
         Console.WriteLine(authResult.CreateAuthorizationHeader());
         httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(authResult.CreateAuthorizationHeader());


         const string environmentsUri = "https://management.azure.com/subscriptions?api-version=2020-01-01";

         var response = httpClient.GetAsync(environmentsUri).Result;

         var content = response.Content.ReadAsStringAsync().Result;
         Console.WriteLine(content);

     }


But when I execute the code, it seems that I have an authentication error:
- {"error":{"code":"AuthenticationFailed","message":"Authentication failed."}}

microsoft-authenticatorazure-ad-authentication
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers