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", "https://analysis.windows.net/powerbi/api"},
            {"redirect_uri", "http://localhost:13526/Redirect"}
        };


        var queryString = HttpUtility.ParseQueryString(string.Empty);
        queryString.Add(@params);

        string authorityUri = "https://login.windows.net/common/oauth2/authorize/";
        var authUri = String.Format("{0}?{1}", authorityUri, queryString);
        Response.Redirect(authUri);


        Redirect.aspx
        string redirectUri = "http://localhost:13526/Redirect";
        string authorityUri = "https://login.windows.net/common/oauth2/authorize/";

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

        TokenCache TC = new TokenCache();

        AuthenticationContext AC = new AuthenticationContext(authorityUri, TC);
        ClientCredential cc = new ClientCredential
            (Properties.Settings.Default.ClientID,
            Properties.Settings.Default.ClientSecret);

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

        Session[_Default.authResultString] = AR;

        Response.Redirect("/Default.aspx");
        Default.aspx
         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
108 views

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 https://login.microsoftonline.com/tenant-id/. Here is the response class I'm using:

class AzureAdTokenResponse
{
    [JsonProperty("access_token")]
    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
• 40,440 points
67 views
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
• 14,070 points
97 views
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,530 points
241 views
0 votes
1 answer

Passing a parameter to Power BI Embedded via embedded URL

Add the following to the end of ...READ MORE

answered Oct 23, 2018 in Power BI by Annie97
• 2,190 points
1,044 views
0 votes
1 answer

powerbi embeded in .net core 1.1

See if it helps: https://www.nuget.org/packages/PowerBI.NetStandard ...READ MORE

answered Nov 28, 2018 in Power BI by Upasana
• 8,530 points
89 views
+1 vote
4 answers

Where to find the Microsoft.IdentityModel dll?

I finally found this Nuget package: https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM, which ...READ MORE

answered Oct 23, 2018 in Azure by Trisha
505 views
0 votes
4 answers

How can I rename a SQL Azure database?

This command serves the purpose ALTER DATABASE [oldname] ...READ MORE

answered Nov 30, 2018 in Azure by Abhinav
364 views
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,130 points
651 views
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,190 points
889 views