Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
rohanP
Microsoft Employee
Microsoft Employee

problem while getting authentication token for native active directory app using .Net Core

Hi,

We are working on Power BI Report embedding in .Net Core web app. We are facing issue while generating auth token for native Active Directory app registered on Azure. We can able to embed the Power Bi report in .Net 4.6 web app, but facing issue in .Net core web app.

To generate token we are using REST API and calling then using HTTPClient but this its returns error as "Invaliad credentials". I have triple checked the credentials and those are correct. We follow the example provide here to generate token.

 
Code snippet : 
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", "b473122eXXXXXXXXX"),
                    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 = result.Content.ReadAsStringAsync().Result;
                return JsonConvert.DeserializeObject<OAuthResult>(content);
            }

 

 

Can you please provide any alteranative or solution or pointers to get auth token for Native ADD app using .Net core.

 

Regards,

Rohan

1 REPLY 1
v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi Rohan,

 

Since we call the REST API directly, the "oauthEndpoint" is different. Please refer to the code below, which works in my test.

 

        private static async Task<OAuthResult> AuthenticateAsync()
        {
            String clientId = "3a7c5ccc-***********5f";
            String authorityUrl = "https://login.microsoftonline.com/{replace_with_your_tenant_id}/oauth2/token";
            String resourceUrl = "https://analysis.windows.net/powerbi/api";
            String userName = "da*****oft.com";
            String password = "P***1";

            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();
             
                return JsonConvert.DeserializeObject<OAuthResult>(content);
            }

        }

1. Regarding the tenant ID, please refer to how-to-get-the-azure-account-tenant-id.

 

2. Get Azure AD access token, please refer to /azure/active-directory/develop/v1-protocols-oauth-code#use-the-authorization-code-to-request-an-acc....

 

 

Please understand that this link is provided “AS IS” with no warranties or guarantees of content changes, and confers no rights.

 

Best Regards,
Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.