Quantcast
Channel: Developer topics
Viewing all articles
Browse latest Browse all 17884

problem getting authentication token from AAD

$
0
0

Hello,

                I’m trying to embed a report for non power bi users (existing ASP.NET web form app, not MVC). Basically new aspx page should be added to an existing asp.net app and report should be rendered upon page load event.

 

                 I’m stuck on getting authentication token from AAD. New app was registered in AAD as Native, permissions were granted. The problem is that the code below freezes on AcquireTokenAsync call and nothing is returned.

 

public static async Task<AuthenticationResult> Authenticate(string authorityUrl, string resourceUrl, string clientId, string userName, string password)
        {
            UserPasswordCredential credential = new UserPasswordCredential(userName, password);
            AuthenticationContext authContext = new AuthenticationContext(authorityUrl);
            var authResult = await authContext.AcquireTokenAsync(resourceUrl, clientId, credential);
            return authResult;
        }

 

 

Call to AcquireTokenAsync with the same parameters works fine in a console app returning access token:

 

 

static void Main(string[] args)
        {
            Console.WriteLine(Authenticate().Result.AccessToken);
            Console.ReadLine();
        }
        private static async Task<AuthenticationResult> Authenticate()
        {
            string userName = "{user}";
            string password = "{password}";
            string clientId = "{AAD client id}";
            var credentials = new UserPasswordCredential(userName, password);
            var authenticationContext = new AuthenticationContext("https://login.windows.net/common/oauth2/authorize/");
            var result = await authenticationContext.AcquireTokenAsync("https://analysis.windows.net/powerbi/api", clientId, credentials);
            return result;
        }

 

 

It seems that Azure AD setup is correct – I was able to use App Owns Data git demo, got access token in HomeController, and embed report with no issues.

  

I have also tried to use approach described in this link and used OAuthResult native REST call to the Azure AD but again, code freezes on client.PostAsync call:

 

public static async Task<OAuthResult> Authenticate(string authorityUrl, string resourceUrl, string clientId, string userName, string password)
        {
            var oauthEndpoint = new Uri(authorityUrl);
            OAuthResult authResult = new OAuthResult();

            using (var client = new HttpClient())
            {
                var 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"),
                }));
                var content = await result.Content.ReadAsStringAsync();
                authResult =  JsonConvert.DeserializeObject<OAuthResult>(content);
            }
            return authResult;
        }

 

Can someone point to me to what is wrong or missing?

 

Thank you very much.


Viewing all articles
Browse latest Browse all 17884

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>