cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Microsoft
Microsoft

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
Microsoft
Microsoft

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
secondImage

Happy New Year from Power BI

This is a must watch for a message from Power BI!

December Update

Check it Out!

Click here to read more about the December 2020 Updates!

Community Blog

Check it Out!

Click here to read the latest blog and learn more about contributing to the Power BI blog!

Get Ready for Power BI Dev Camp

Get Ready for Power BI Dev Camp

Mark your calendars and join us for our next Power BI Dev Camp!.