Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

MSAL with unity3d?

Discussion in 'Windows' started by young-xyz, Jan 3, 2022.

  1. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    51
    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?
     
  2. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    51
    anyone?
     
  3. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,504
    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.
     
  4. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    51
    I called it with WSA InvokeOnUIThread like this,

    Code (CSharp):
    1.  
    2.     async void Start()
    3.     {
    4.         UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
    5.         {
    6.             await SignInUserAndGetTokenUsingMSAL(scopes);
    7.         }, false);
    8.     }
    9.  
    10.     private async Task<string> SignInUserAndGetTokenUsingMSAL(string[] scopes)
    11.     {
    12.         PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
    13.             .WithAuthority(Authority)
    14.             .WithUseCorporateNetwork(false)
    15.              .WithLogging((level, message, containsPii) =>
    16.              {
    17.                  Debug.Log($"MSAL: {level} {message} ");
    18.              }, LogLevel.Warning, enablePiiLogging: false, enableDefaultPlatformLogging: true)
    19.             .Build();
    20.         IEnumerable<IAccount> accounts = await PublicClientApp.GetAccountsAsync().ConfigureAwait(false);
    21.         IAccount firstAccount = accounts.FirstOrDefault();
    22.         try
    23.         {
    24.             authResult = await PublicClientApp.AcquireTokenSilent(scopes, firstAccount)
    25.                                               .ExecuteAsync();
    26.         }
    27.         catch (MsalUiRequiredException ex)
    28.         {
    29.             authResult = await PublicClientApp.AcquireTokenInteractive(scopes)
    30.                                               .WithAccount(firstAccount)
    31.                                               .ExecuteAsync()
    32.                                               .ConfigureAwait(false);
    33.         }
    34.         return authResult.AccessToken;
    35.     }
    36.  
    but I got error like this

    msal_run.png

    What is the problem?
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,504
    Which Unity version is this on? Did you enable networking capabilities?
     
  6. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    51
    It is unity 2020.3.15f2 and capabilities setting is like this

    ms_unity.png
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,504
    Is this failing when running on your local machine or are you running on a remote device?
     
  8. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    51
    It is on local machine.
     
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,504
    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.
     
  10. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    51
    I ran it with Unity 2021.2.13f but I get the same message. So I cannot run it properly now?
     
  11. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,504
    I suggest filing a bug report, that way we at least can investigate what is going wrong.
     
  12. Sneha_Pallewar

    Sneha_Pallewar

    Joined:
    Feb 18, 2019
    Posts:
    7
    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
     
  13. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,504
    I would suggest posting that on Android forum as I don't know anything about Android.
     
  14. JCIPL

    JCIPL

    Joined:
    Jul 13, 2018
    Posts:
    14
    We have posted the same question in the Unity's Android forum
    https://forum.unity.com/threads/android-msal-plugin-creation.1271345/

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