title: PowerBI and Azure AD Headless Login
body: <p>I am trying to embed PowerBI dashboards into my customer MVC portal.  My customers don't have AAD accounts, so they can't login to Live when they come to the website, they log into my MVC website with individual authority.  </p>
  
  <p>I have registered my App on PowerBI/AAD and have the ClientID and Secret.  I make the call to AAD and get an Authorization Code which I then use to get an Athentication Token which the is returned successfully.</p>
  
  <p>When ever I use the access token to get a dashboard it is continually rejected with a 403 Forbidden.</p>
  
  <p>I have gone through all the samples from Microsoft, but they require a user login prompt.  I have reviewed the ADAL2.0 code which refers to the AcquireToken Method, but this was deprecated in ADAL3 and replaced with AcquireTokenAsync which has different parameters and I am using this in my example below.</p>
  
  <p>Here is the function to get the token:</p>
  
  <pre><code>    protected AuthenticationResult GetAccessToken()
      {
          string pBiUser = Properties.Settings.Default.PowerBIUser;
          string pBiPwd = Properties.Settings.Default.PowerBIPwd;
          string pBiClientId = Properties.Settings.Default.PowerBIClientId;
          string pBiSecret = Properties.Settings.Default.PowerBIClientSecret;
          TokenCache TC = new TokenCache();
          ClientCredential CC = new ClientCredential(pBiClientId,pBiSecret);
          string AU = Properties.Settings.Default.PowerBIAuthority;
          Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authenticationContext 
              = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(AU, TC);
          AuthenticationResult result = authenticationContext.AcquireTokenAsync("https://analysis.windows.net/powerbi/api"
              ,CC).Result;
  
          if (result == null)
          {
              throw new InvalidOperationException("Failed to obtain the PowerBI token");
          }
  
          return result;
      }
  </code></pre>
  
  <p>I then take the result token and call.  The <strong>response</strong> receives the 403:</p>
  
  <pre><code>    protected PBIDashboards GetDashboards(AuthenticationResult authResult)
      {
          PBIDashboards pbiDashboards = new PBIDashboards();
          var baseAddress = new Uri("https://api.powerbi.com");
          using (var httpClient = new System.Net.Http.HttpClient {BaseAddress = baseAddress})
          {
              httpClient.DefaultRequestHeaders.TryAddWithoutValidation("authorization",
                  "Bearer " + authResult.AccessToken);
              using (**var response** = httpClient.GetAsync("v1.0/myorg/dashboards").Result)
              {
                  string responseData = response.Content.ReadAsStringAsync().Result;
  
                  //Deserialize JSON string
                  pbiDashboards = JsonConvert.DeserializeObject&lt;PBIDashboards&gt;(responseData);
  
                  if (pbiDashboards != null)
                  {
                      var gridViewDashboards = pbiDashboards.value.Select(dashboard =&gt; new
                      {
                          Id = dashboard.id,
                          DisplayName = dashboard.displayName,
                          EmbedUrl = dashboard.embedUrl
                      });
                  }
              }
          }
          return pbiDashboards;
      }
  </code></pre>