1

I'm trying to get some MSFT Power BI SDK samples working. Unfortunately, the Microsoft.IdentityModel.Clients.ActiveDirectory library is giving me a lot of trouble with the initial external authentication step.

I'm using Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.28.3.860, from NuGet; this is the last version of the library before AcquireToken was removed, and I haven't figured out how to use its replacement (AcquireTokenAsync) in a way that's equivalent to what I see in the samples.

When I take the following code and modify the TODO lines to specify my actual Azure Client ID and authentication redirect page, I get as far as the AcquireToken line.

using System;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

public class Application
{
    public static void Main(string[] args)
    {
        try
        {
            string clientID = "abcdef01-1234-1234-abcd-abcdabcd1234"; // TODO: actual Azure client ID
            string redirectUri = "https://acmecorporation.okta.com/login/do-login"; // TODO: actual redirect
            string resourceUri = "https://analysis.windows.net/powerbi/api";
            string authorityUri = "https://login.windows.net/common/oauth2/authorize";

            AuthenticationContext authContext = new AuthenticationContext(authorityUri);
            AuthenticationResult ar = authContext.AcquireToken(
                    resourceUri,
                    clientID,
                    new Uri(redirectUri),
                    PromptBehavior.RefreshSession);
            string token = ar.AccessToken;
            Console.WriteLine("Success: " + token);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

At this point:

  1. A "Sign in to your account" window pops up with the name of the app I've associated in Azure with the clientID GUID
  2. I'm redirected to my organization's ("acmecorporation") sign-on page
  3. I sign in using my AD credentials
  4. The AcquireToken method throws the following NullReferenceExpection:

    System.NullReferenceException: Object reference not set to an instance of an object.
     at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.RunAsyncTask[T](Task`1 task)
     at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.AcquireToken(String resource, String clientId, Uri redirectUri, PromptBehavior promptBehavior)
     at PowerBISample.Application.Main(String[] args) in \\noxfile\users\ehirst\documents\visual studio 2015\Projects\PowerBISample\PowerBISample\Program.cs:line 18
    

Can anyone provide guidance on how to get past this? My goal is to get a POC working to determine whether we can integrate Power BI into a larger application, but so far it feels like I'm beta testing a pretty unstable system.

Eric Hirst
  • 1,111
  • 15
  • 31
  • you should file a bug with full stacktrace at https://github.com/AzureAD/azure-activedirectory-library-for-dotnet – Kanishk Panwar Dec 29 '16 at 17:13
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Heretic Monkey Dec 29 '16 at 17:39
  • Thanks @KanishkPanwar-MSFT. Yes; I'm new to both Power BI and ADAL. Knowing that the problem is in the ADAL open source code should be enough to go forward. – Eric Hirst Dec 29 '16 at 18:17
  • @MikeMcCaughan I edited the question and removed the C# tag to clarify. – Eric Hirst Dec 29 '16 at 18:43

1 Answers1

2

The NullReferenceException is a bug in the 2.x version of the ADAL library; it's fixed in current versions. It was triggered by an incorrect value of redirectUri; unfortunately the documentation was sparse on this one. A working code sample, adapted (thanks Kanishk!) to use the current 3.13.7 version of ADAL, is posted below.

namespace PowerBISample
{
    using System;
    using Microsoft.IdentityModel.Clients.ActiveDirectory;
    using System.Threading.Tasks;

    public class Application
    {
        public static void Main(string[] args)
        {
            Run();
            Console.ReadLine();
        }

        static async void Run()
        {
            try
            {
                string clientID = "abcdef01-1234-1234-abcd-abcdabcd1234"; // TODO: actual Azure client ID

                 /** THE REAL PROBLEM WAS HERE **/
                string redirectUri = "https://login.live.com/oauth20_desktop.srf";

                string resourceUri = "https://analysis.windows.net/powerbi/api";
                string authorityUri = "https://login.windows.net/common/oauth2/authorize";

                AuthenticationContext authContext = new AuthenticationContext(authorityUri);

                AuthenticationResult ar = await authContext.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri), new PlatformParameters(PromptBehavior.RefreshSession));
                string token = ar.AccessToken;
                Console.WriteLine("Success: " + token);
            }
            catch (Exception ex)
            {
                string error = ex.ToString();
                Console.WriteLine(error);
            }
        }
    }
}
Eric Hirst
  • 1,111
  • 15
  • 31
  • With 3.1.15 I got the error "Key not valid for use in specified state", but upgrading to 5.0.5 seems to have solved all the issues I was having so thanks! :-) – SharpC Apr 19 '23 at 13:34