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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
toy
Advocate I
Advocate I

authentication issue when embedding in aspnetcore

building on this previous post:

https://community.powerbi.com/t5/Developer/Embed-Power-BI-dashboard-in-ASP-Net-core/td-p/273279

(Embed Power BI dashboard in ASP.Net core)

 

on this line:

                string content = await result.Content.ReadAsStringAsync();


the content im receiving back in AuthenticateAsync is this (only a portion):

"\r\n\r\n<!DOCTYPE html>\r\n<html dir=\"ltr\" class=\"\" lang=\"en\">\r\n<head>\r\n    <title>Sign in to your account</title>\r\n    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\r\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes\">\r\n    <meta http-equiv=\"Pragma\" content=\"no-cache\">\r\n    <meta http-equiv=\"Expires\" content=\"-1\">\r\n    <meta name=\"PageID\" content=\"ConvergedSignIn\" />\r\n    <meta name=\"SiteID\" content=\"\" />\r\n    <meta name=\"ReqLC\" content=\"1033\" />\r\n    <meta name=\"LocLC\" content=\"en-US\" />\r\n\r\n        <noscript>\r\n            <meta http-equiv=\"Refresh\" content=\"0; URL=https://login.windows.net/common/jsdisabled\" />\r\n        </noscript>\r\n        <link rel=\"shortcut icon\" href=\"https://secure.aadcdn.microsoftonline-p.com/ests/2.1.7382.8/content/images/favicon_a.ico\" />\r\n    \r\n    "

 

which needless to say is not JSON and certainly isnt a token!

can anyone tell me whats going on here? and what is my next step to getting a tooken?

 

here is the rendered outut returned from the function:

mslogin.png

 

 

ps - i know my credentials work 

because i built the example in the standard .net version (ie minus the mods for aspnetcore)

and they totally worked

 

 

1 ACCEPTED SOLUTION

endpoints have changed:

AuthorityUrl = "https://login.windows.net/common/oauth2/token/

 

in one of the examples it was 

AuthorityUrl = "https://login.windows.net/common/oauth2/authorize/

which is what i had in my code

 

hope this helps

 

 

 

View solution in original post

5 REPLIES 5
v-micsh-msft
Employee
Employee

Follow the steps mentioned in the thread below:

Embed Power BI Report in Asp.Net Core Website

 

 The workaround here is to compose the HTTP request directly.

Not sure how the code in your tests works,the content you received should be the authentication page from "login.windows.net", which is used to finish the authentication and generate the token.

The workable content should be the content returned from "login.windows.net".

 

Regards,

Michael

 

hey michael 

thx for the response

but if you read my orig post

i stated i had already followed that post

and the result im getting is the result i posted

(adjusted so as to render the non-json result i am getting)

 

for clarity here is my current code:

       public async Task<ActionResult> index() {
            var result = new EmbedConfig { Username = Username };
            var error = GetWebConfigErrors();
            if (error != null)
            {
                result.ErrorMessage = error;
                return View(result);
            }

            var authenticationResult = await AuthenticateAsync();
            if (authenticationResult == null) {
                result.ErrorMessage = "Authentication Failed.";
                return View(result);
            }
            else
            {
                return View(new connectVM(
                                isJson(authenticationResult) ? JsonConvert.DeserializeObject<string>(authenticationResult) : authenticationResult));
            }

            //var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
        }


        //.private static async Task<OAuthResult> AuthenticateAsync()
        // JsonConvert.DeserializeObject<OAuthResult>(authenticationResult) 
        private static async Task<string> AuthenticateAsync()
        {
            Uri oauthEndpoint = new Uri(AuthorityUrl);
            using (HttpClient client = new HttpClient())
            {
                HttpResponseMessage 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"),
                }));

                string content = await result.Content.ReadAsStringAsync();
                //Console.WriteLine(content.ToString());
                return content.ToString();
            }
        }

 

UPDATE:

ive tried logging in directly through browser url

here is the url im using:

https://login.windows.net/common/oauth2/authorize/?client_id=myid&grant_type=password&username=myun&...

 

does the url at least look correct for the non-sensitive info?

still getting the sign in box

I'm having the exactly same issue, with the same code. Have also tried to get the token using Postman.

Always getting the Sign in page as response.

 

Here is the request:

POST /common/oauth2/authorize/ HTTP/1.1
Host: login.windows.net
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache

client_id=clientId&
resource=https%3A%2F%2Fanalysis.windows.net%2Fpowerbi%2Fapi&
grant_type=password&
username=user@domain&
password=userpassword&
scope=openid

 

endpoints have changed:

AuthorityUrl = "https://login.windows.net/common/oauth2/token/

 

in one of the examples it was 

AuthorityUrl = "https://login.windows.net/common/oauth2/authorize/

which is what i had in my code

 

hope this helps

 

 

 

Thank you! Was the endpoint 🙂

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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