MSAL with unity3d?

I’m trying to the Microsoft auth with unity3d uwp.

Start from Azure quick start page sample, it worked good as itself.

But in unity3d build, AcquireTokenInteractive is not works.

How do I get it to work?

anyone?

One gotcha that you should check for is which thread you’re calling the APIs from. The API name suggests you should be calling it from the UI thread.

I called it with WSA InvokeOnUIThread like this,

    async void Start()
    {
        UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
        {
            await SignInUserAndGetTokenUsingMSAL(scopes);
        }, false);
    }

    private async Task<string> SignInUserAndGetTokenUsingMSAL(string[] scopes)
    {
        PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
            .WithAuthority(Authority)
            .WithUseCorporateNetwork(false)
             .WithLogging((level, message, containsPii) =>
             {
                 Debug.Log($"MSAL: {level} {message} ");
             }, LogLevel.Warning, enablePiiLogging: false, enableDefaultPlatformLogging: true)
            .Build();
        IEnumerable<IAccount> accounts = await PublicClientApp.GetAccountsAsync().ConfigureAwait(false);
        IAccount firstAccount = accounts.FirstOrDefault();
        try
        {
            authResult = await PublicClientApp.AcquireTokenSilent(scopes, firstAccount)
                                              .ExecuteAsync();
        }
        catch (MsalUiRequiredException ex)
        {
            authResult = await PublicClientApp.AcquireTokenInteractive(scopes)
                                              .WithAccount(firstAccount)
                                              .ExecuteAsync()
                                              .ConfigureAwait(false);
        }
        return authResult.AccessToken;
    }

but I got error like this

What is the problem?

Which Unity version is this on? Did you enable networking capabilities?

It is unity 2020.3.15f2 and capabilities setting is like this

Is this failing when running on your local machine or are you running on a remote device?

It is on local machine.

Pretty bad news. It looks like that MSAL library uses non-trivial System.Net APIs and historically our support for them has been spotty. In Unity 2021.2, we upgraded to newer Mono and class library versions, it might be worth trying it on that Unity version. If you report a bug, we could investigate it but it’s unlikely we will be able to backport a fix to 2020.3 LTS unless it’s something simple, as changes to make this work could be extensive. Sorry.

I ran it with Unity 2021.2.13f but I get the same message. So I cannot run it properly now?

I suggest filing a bug report, that way we at least can investigate what is going wrong.

Tautvydas-Zilys We are trying to create a plugin for android platform using MSAL Authentication library. But while creating plugin MSAL need gradle version ‘7.0.0’ and we are using unity version 2020.3.11. In unity documents it says that unity supports the gradle version ‘6.1.1’.

Could you please suggest how we can use gradle ‘7.0.0’ in unity

I would suggest posting that on Android forum as I don’t know anything about Android.

We have posted the same question in the Unity’s Android forum
[ https://discussions.unity.com/t/879155 ](http:// https://discussions.unity.com/t/879155 )

Unfortunately, we are not getting any replies there. Could you please tag anyone who can help us on the same