Authorization in Azure Active Directory without using dialog

+1 vote

The thing is that my application shows a dashboard of my Power BI account for all users. I am authorizing the Azure Active Directory through a dialog to get an access token. 

So, can I hard code my credentials and get access token without using the authorization dialog code?

It works but it is using the authorization dialog.

 var @params = new NameValueCollection
            {"response_type", "code"},
            {"client_id", Properties.Settings.Default.ClientID},
            {"resource", ""},
            {"redirect_uri", "http://localhost:13526/Redirect"}

        var queryString = HttpUtility.ParseQueryString(string.Empty);

        string authorityUri = "";
        var authUri = String.Format("{0}?{1}", authorityUri, queryString);

        string redirectUri = "http://localhost:13526/Redirect";
        string authorityUri = "";

        string code = Request.Params.GetValues(0)[0];

        TokenCache TC = new TokenCache();

        AuthenticationContext AC = new AuthenticationContext(authorityUri, TC);
        ClientCredential cc = new ClientCredential

        AuthenticationResult AR = AC.AcquireTokenByAuthorizationCode(code, new Uri(redirectUri), cc);

        Session[_Default.authResultString] = AR;

         string responseContent = string.Empty;

        System.Net.WebRequest request = System.Net.WebRequest.Create(String.Format("{0}dashboards", baseUri)) as System.Net.HttpWebRequest;
        request.Method = "GET";
        request.ContentLength = 0;
        request.Headers.Add("Authorization", String.Format("Bearer {0}", authResult.AccessToken));

        using (var response = request.GetResponse() as System.Net.HttpWebResponse)
            using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
                responseContent = reader.ReadToEnd();
                PBIDashboards PBIDashboards = JsonConvert.DeserializeObject<PBIDashboards>(responseContent);

Oct 25, 2018 in Power BI by Annie
• 160 points

1 answer to this question.

0 votes

I once happened to do this without using ADAL. For Power BI as well, since they don't offer application permissions, only delegated.

What you need to is call the AAD token endpoint with grant_type=password. You will specify the username and password, as well as the client id, client secret and resource URI in form parameters.

Here is the function:

private async Task<string> GetAccessToken()
    string tokenEndpointUri = Authority + "oauth2/token";

    var content = new FormUrlEncodedContent(new []
            new KeyValuePair<string, string>("grant_type", "password"),
            new KeyValuePair<string, string>("username", Username),
            new KeyValuePair<string, string>("password", Password),
            new KeyValuePair<string, string>("client_id", ClientId),
            new KeyValuePair<string, string>("client_secret", ClientSecret),
            new KeyValuePair<string, string>("resource", PowerBiResourceUri)

    using (var client = new HttpClient())
        HttpResponseMessage res = await client.PostAsync(tokenEndpointUri, content);

        string json = await res.Content.ReadAsStringAsync();

        AzureAdTokenResponse tokenRes = JsonConvert.DeserializeObject<AzureAdTokenResponse>(json);

        return tokenRes.AccessToken;

Authority here is Here is the response class I'm using:

class AzureAdTokenResponse
    public string AccessToken { get; set; }
answered Oct 25, 2018 by Surendra

Related Questions In Power BI

0 votes
1 answer

Can I create a Power BI report in Office 365 without my locally installed Office Excel?

Yess!! You can achieve this by using ...READ MORE

answered Sep 26, 2018 in Power BI by Kalgi
• 51,970 points
0 votes
1 answer

list reports with calculated percentage in Power BI using dax

Create a Measure called Total Revenue: Total Revenue = SUMX( ...READ MORE

answered Oct 22, 2018 in Power BI by Hannah
• 18,420 points
0 votes
1 answer

Error:connecting to Azure SSAS server in Power-BI through Import mode

If I understand correctly, you're connecting to ...READ MORE

answered Nov 9, 2018 in Power BI by Upasana
• 8,620 points
0 votes
1 answer

Applying multiple filters in PBI Embed URL

Well, that is actually a limitation in ...READ MORE

answered Aug 21, 2018 in Power BI by DataKing99
• 8,210 points
0 votes
1 answer

Embed URL-multiple filters in Power BI

With the release of 2.0.0 you can ...READ MORE

answered Oct 16, 2018 in Power BI by Annie97
• 2,200 points