1

I'm building Azure AD authentication into my Android Application, using MSAL 3.x, however it is throwing errors when attempting to Acquire Token with User Interaction. The documentation for MSAL 3.x suggests that the parent activity or window is needed, however using Unity I'm pretty lost with this. Any help here would be much appreciated.

I have tried a variety of input suggestions from the main documentation, but none of them seems to fit for Unity. The documentation can be found at: https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/Acquiring-tokens-interactively

authResult = await app.AcquireTokenInteractive(scopes).ExecuteAsync();
authResult = await app.AcquireTokenInteractive(scopes).WithParentActivityOrWindow("need something here").ExecuteAsync();
JolofNar
  • 43
  • 6

1 Answers1

0

Unity uses UnityPlayerActivity or UnityPlayerNativeActivity depending on the version.

The Activity is mandatory in MSAL.NET for Android applications because it is the required context to create the process to acquire the token interactively.

For other situations, you may want to use the main/launcher activity.

For Unity, you may want to look at extending a Unity Activity and creating a hook for your MSAL.NET code.

  • Hi Adam, could you elaborate a little. I am on 2018.3, are you saying that the UnityPlayerActivity/UnityPlayerNativeActivity can be used as an activity. Thanks for the help. – JolofNar May 15 '19 at 08:11
  • I have attempted to use `AndroidJavaClass jclass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject activity = jclass.GetStatic("currentActivity");` to get a reference to the current activity. It isn't working, is that the right activity to get access to? Do I need more? Or is this even the right area to look in? – JolofNar May 15 '19 at 13:23
  • Hi JolofNar, this is very similar to this post: https://stackoverflow.com/questions/10069340/how-to-start-an-android-activity-from-a-unity-application. This is the right track--you need that reference to the Activity. – Adam Colclough May 15 '19 at 18:11
  • Thanks Adam, this has been very helpful so far. But what do I do with the reference to the activity? Do I have to pass it into java and then get it back? `AndroidJavaClass jclass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject activity = jclass.GetStatic("currentActivity"); authResult = await app.AcquireTokenInteractive(scopes).WithParentActivityOrWindow(activity).ExecuteAsync();` is where I have got to so far, but that isn't working. Is it because the reference I'm passing in is an AndroidJavaObject rather than an Activity? – JolofNar May 16 '19 at 08:19
  • ps, you may have to lay it out super simply for me, as I have no experience with java outside of this project. – JolofNar May 16 '19 at 09:25
  • Follow up question, I've tried building a plugin for java and have now got the activity referenced back into unity as an androidjavaobject, however it is throwing up errors, and not recognising it as an activity for withParentActivityOrWindow. Is it possible to move beyonf this step? – JolofNar May 21 '19 at 15:37