Introduction
We need an AD authenticated access token to embed PowerBI Reports in our application. In the past, I embedded the PowerBI Reports in Angular with ASP.NET MVC using some NuGet packages to get the access token. But recently, I am working with .NET Core applications where these NuGet packages do not work. In this article, I will explain a workaround or an alternative to the NuGet approach.
Getting Access Token Using NuGet In ASP.NET MVC
Earlier, with ASP.NET MVC, we were using the below NuGet package to get the access token.

The code looks like below.
- private static readonly string Username = Common.Constants.PowerBiConfig.pbiUsername;
- private static readonly string Password = Common.Constants.PowerBiConfig.pbiPassword;
- private static readonly string AuthorityUrl = Common.Constants.PowerBiConfig.authorityUrl;
- private static readonly string ResourceUrl = Common.Constants.PowerBiConfig.resourceUrl;
- private static readonly string ClientId = Common.Constants.PowerBiConfig.clientId;
- var credential = new UserPasswordCredential(Username, Password);
- // Authenticate using created credentials
- var authenticationContext = new AuthenticationContext(AuthorityUrl);
- var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);
Getting Access Token In .NET Core
To overcome this issue, the quick alternative or workaround will be to call the REST API and get an access token. Just replace the above code with this code below.
- private static readonly string AuthorityUrl = @"https://login.windows.net/common/oauth2/token/";
- var oauthEndpoint = new Uri(AuthorityUrl);
- using (var client = new HttpClient())
- {
- var result = await client.PostAsync(oauthEndpoint, new FormUrlEncodedContent(new[]
- {
- new KeyValuePair<string, string>("resource", ResourceUrl),
- new KeyValuePair<string, string>("client_id", ClientId),
- new KeyValuePair<string, string>("grant_type", "password"),
- new KeyValuePair<string, string>("username", Username),
- new KeyValuePair<string, string>("password", Password),
- new KeyValuePair<string, string>("scope", "openid"),
- }
- ));
- var content = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
- var resp = JsonConvert.DeserializeObject<OAuthResultModel>(content);
- public class OAuthResultModel
- {
- [JsonProperty("token_type")]
- public string TokenType { get; set; }
- [JsonProperty("scope")]
- public string Scope { get; set; }
- [JsonProperty("experies_in")]
- public int ExpiresIn { get; set; }
- [JsonProperty("ext_experies_in")]
- public int ExtExpiresIn { get; set; }
- [JsonProperty("experies_on")]
- public int ExpiresOn { get; set; }
- [JsonProperty("not_before")]
- public int NotBefore { get; set; }
- [JsonProperty("resource")]
- public Uri Resource { get; set; }
- [JsonProperty("access_token")]
- public string AccessToken { get; set; }
- [JsonProperty("refresh_token")]
- public string RefreshToken { get; set; }
- }
Summary
In this article, I discussed how to resolve the AD access token issue in .NET Core and the quick alternative to get access token. It took a half of a day to search and resolve this issue. Now, I hope it will save a lot of your time and will be helpful.

Narmada PatthiPosted Oct 21, 2021, 4:09 PM
This article really helped. Thanks a lot !!
Luis NogueraPosted Dec 15, 2020, 11:39 AM
Not working in .net core 3.1. The HttpClient respond with a bad request.
saurabh abhyankarPosted Aug 19, 2019, 6:46 AM
The resource principal named https://analysis.windows.net/powerbi/api/ was not found in the tenant named ****.com. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant.
saurabh abhyankarPosted Aug 19, 2019, 6:45 AM
Hi I seem to get this error - AADSTS500011