How To Get AD Access Token For Power BI Embedding In .NET Core

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.

 
How To Get AD Access Token For Power BI Embedding In .NET Core
 
The code looks like below.
  1. private static readonly string Username = Common.Constants.PowerBiConfig.pbiUsername;  
  2.       private static readonly string Password = Common.Constants.PowerBiConfig.pbiPassword;  
  3.       private static readonly string AuthorityUrl = Common.Constants.PowerBiConfig.authorityUrl;  
  4.       private static readonly string ResourceUrl = Common.Constants.PowerBiConfig.resourceUrl;  
  5.       private static readonly string ClientId = Common.Constants.PowerBiConfig.clientId;  
  1. var credential = new UserPasswordCredential(Username, Password);  
  2.   
  3.                // Authenticate using created credentials  
  4.                var authenticationContext = new AuthenticationContext(AuthorityUrl);  
  5.                var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);  
The above code will not work in .NET Core.
 

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.
  1. private static readonly string AuthorityUrl = @"https://login.windows.net/common/oauth2/token/";  
  1. var oauthEndpoint = new Uri(AuthorityUrl);  
  2.   
  3.              using (var client = new HttpClient())  
  4.              {  
  5.                  var result = await client.PostAsync(oauthEndpoint, new FormUrlEncodedContent(new[]  
  6.                  {  
  7.                 new KeyValuePair<string, string>("resource", ResourceUrl),  
  8.                 new KeyValuePair<string, string>("client_id", ClientId),  
  9.                 new KeyValuePair<string, string>("grant_type""password"),  
  10.                 new KeyValuePair<string, string>("username", Username),  
  11.                 new KeyValuePair<string, string>("password", Password),  
  12.                 new KeyValuePair<string, string>("scope""openid"),  
  13.                     }  
  14.                  ));  
  15.   
  16.                  var content = await result.Content.ReadAsStringAsync().ConfigureAwait(false);  
  17.                  var resp = JsonConvert.DeserializeObject<OAuthResultModel>(content);  
  1. public class OAuthResultModel  
  2.    {  
  3.        [JsonProperty("token_type")]  
  4.        public string TokenType { get; set; }  
  5.        [JsonProperty("scope")]  
  6.        public string Scope { get; set; }  
  7.        [JsonProperty("experies_in")]  
  8.        public int ExpiresIn { get; set; }  
  9.        [JsonProperty("ext_experies_in")]  
  10.        public int ExtExpiresIn { get; set; }  
  11.        [JsonProperty("experies_on")]  
  12.        public int ExpiresOn { get; set; }  
  13.        [JsonProperty("not_before")]  
  14.        public int NotBefore { get; set; }  
  15.        [JsonProperty("resource")]  
  16.        public Uri Resource { get; set; }  
  17.        [JsonProperty("access_token")]  
  18.        public string AccessToken { get; set; }  
  19.        [JsonProperty("refresh_token")]  
  20.        public string RefreshToken { get; set; }  
  21.    }  
This will solve the access token issue in .NET Core.
 

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.


Similar Articles