Forum Discussion

toy's avatar
toy
Advocate I
8 years ago
Solved

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:

 

 

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

 

 

5 Replies

  • v-micsh-msft's avatar
    v-micsh-msft
    Microsoft 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

     

    • toy's avatar
      toy
      Advocate I

      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&password=mypw&scope=openid

       

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

      still getting the sign in box

      • Ameb's avatar
        Ameb
        Helper I

        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