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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

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
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.