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."}}