0

I'm trying to authenticate with MSAL library. What is the purpose of "object parent" in the function call? Best I found was it's supposed to be the parent Window. I've tried putting this (dumb, I know) and I've tried FindWindow(null, "AppName") (from user32.dll) to get an IntPtr to the window of the app, but everything yields a null error when calling the function. So what should go there to enable this function to work? Does Unity have a specific handle to whatever this should be?

And why in the world do the docs not at all mention this required parent?? https://learn.microsoft.com/en-us/dotnet/api/microsoft.identity.client.ipublicclientapplication.acquiretokeninteractive?view=azure-dotnet

Edit: I was mostly concerned with what the "object parent" is supposed to be. Without knowing what it's supposed to be, I don't know how to call the function. As seen in the image below, Visual Studio is flagging the call without it and forcing me to put it in. I am now including the rest of the code in case someone wishes to point out something else entirely that I may be doing wrong apart from the question regarding the use of "parent".

    using Microsoft.Identity.Client;

    public async void SharepointLogin()
    {
        IntPtr windowPtr = FindWindow(null, "MyAppName"); // This is the "parent" object that I have no idea what it's supposed to be as it is not documented.
        IPublicClientApplication PublicClientApp;
        Debug.Log("Creating PublicClientApplicationBuilder");
        PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
                .WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
                .WithAuthority(AzureCloudInstance.AzurePublic, Tenant)
                .Build();
        Debug.Log("Getting Accounts");
        var accounts = await PublicClientApp.GetAccountsAsync();
        IEnumerable<string> scopes = new List<string> { "User.Read" };
        AuthenticationResult result;
        try
        {
            Debug.Log("Trying Silent Token");
            result = await PublicClientApp.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
                        .ExecuteAsync();
        }
        catch (MsalUiRequiredException)
        {
            Debug.Log("Failed. Trying Interactive Token");
            // This is the line where I can't remove the "parent" object 
            // (in this case "windowPtr") without the error seen in the image 
            // below. It has no compile problems like this but it always has a 
            // null pointer exception when it runs. Everything is the same in the 
            // first "Try" except this "parent" object I'm forced to supply.
            result = await PublicClientApp.AcquireTokenInteractive(scopes, windowPtr)
                        .ExecuteAsync();
        }
    }

enter image description here

CodeMonkey
  • 1,795
  • 3
  • 16
  • 46
  • Since I am unable to mock run your code. I am left to ask obvious questions, are you sure, that `scopes` is a `System.Collections.Generic.IEnumerable` ? Based on the error, this error, is entirely due to a syntax problem. **The reason it's not documented is because it actually isn't required.** [Research](https://stackoverflow.com/questions/56796024/authenticating-against-azure-magment-api-sdk-with-msal-net) – Security Hound Jun 08 '20 at 15:48
  • As you can see in the image, Visual Studio is absolutely requiring that I put "something" in the parent spot. As for the scopes, yes, it is very much that. So I can only assume it must have something to do with the one item that I have no idea what it's supposed to be used for. – CodeMonkey Jun 08 '20 at 15:52
  • `AcquireTokenInteractive` absolutely does not require it. The error message is just not clear. Since you have failed to provided the minium amount of code to diagnose your problem, I am once again going to ask a stupid question, is `PublicClientApp` defined as a `IPublicClientApplication`? Likewise, is `result` defined as a `AuthenticationResult `? Additionally, what is the value of `scopes`? – Security Hound Jun 08 '20 at 16:00
  • See Update. Since Visual Studio is indeed forcing the use of "parent" then perhaps that is somehow my issue. No clue how that could be since I didn't write the dll though...? The error goes away if I put something there and is fine with my IEnumerable. – CodeMonkey Jun 08 '20 at 16:42
  • @CodeMonkey can you verify you are using Microsoft.Identiy.Client v4.14.0 as you linked there is no param for parent mentioned and I can see that it wants a generic object in your screenshot which isn't overly helpful as you mentioned also. I would recommend checking you are on the right documentation for the version you are using. Another thing to try is to right click the method and see the source. A lot of times there will be an explanation above the method declaration. – Skyler Lauren Jun 08 '20 at 17:37
  • Can't see the source and there are no comments. How can I see which version I'm using? – CodeMonkey Jun 08 '20 at 17:41
  • I managed to find the version that Visual Studio is comparing against and it is indeed v4.14.0. So that is super weird that it doesn't match the documentation! – CodeMonkey Jun 08 '20 at 18:08

1 Answers1

1

@Skyler Lauren was on the right track. Though Visual Studio claimed I had 4.14.0 installed, whatever version of the dll I had copied (3.0) from when he answered Visual Studio Understands but Unity Doesn't? ended up trumping the version VS was using. Deleting the one I copied into the Unity folder, uninstalling, reinstalling, and following the function to determine where the REAL dll existed resulted in me no longer needing a "parent" object. Doesn't answer the question what the "parent" was in the first place, but that's now a moot point since I was using the wrong dll. It also doesn't solve my problem with null exception, but that will now be a different question.

CodeMonkey
  • 1,795
  • 3
  • 16
  • 46