2

We develop a bunch of augmented reality applications for our company. The applications are based on the game engine Unity (currently in the version 2020.3.7f1). Now, we're facing the point that we'd like to implement a SSO method in our apps, so every member of our company authenticates himself with his company user credentials. To authenticate the user we want to / have to use the Microsoft identity platform and thus the Microsoft Authentication Library (MSAL).

We already created an client on Azure with a client id, redirect uri, authority uri, endpoint, tenant, etc and it was tested successfully with an official sample project by Microsoft (https://learn.microsoft.com/en-us/azure/active-directory/develop/quickstart-v2-ios).

But now we stuck at the point to establish this process in Unity. We created the MSAL library dll (https://github.com/AzureAD/microsoft-authentication-library-for-dotnet), imported it into Unity and called it by using the namespace Microsoft.Identity.Client. Due to security reasons we're only allowed to use PublicClientApplicationBuilder (which makes totally sense) so we need the web based login screen. The problem is that we can't implement a web view in Unity where the user can sign in to Microsoft and we can parse the received token out of the web view into our app for further communications.

We did a lot of internet research to find a solution but there are only some posts about MSAL in Unity in combination with Android and other posts which reference to the git repos I mentioned before.

How can we use MSAL in Unity for iOS devices?

Example code:

 IPublicClientApplication PublicClientApp = PublicClientApplicationBuilder
        .Create(kClientID)
        .WithRedirectUri(kRedirectUri)
        .WithAuthority(new Uri(kAuthority))
        .Build();
 IEnumerable<string> scopes = new List<string> { "https://graph.microsoft.com/.default" };
 AuthenticationResult result;
 result = await PublicClientApp.AcquireTokenInteractive(scopes)
                   .ExecuteAsync();

 result = await PublicClientApp
        .AcquireTokenInteractive(scopes)
        .WithParentActivityOrWindow(new object)
        .ExecuteAsync();

 Debug.Log(result.AccessToken);

3 Answers 3

0

The standard solution for mobile is the AppAuth pattern, which involves use of an integrated system browser to sign the user in. See this code example article for how that looks, so that you understand the required behaviour.

So it looks like they key thing to do with iOS is to avoid logins via webviews and tell MSAL to use an ASWebAuthenticationSession window - this Microsoft article explains how.

0

we did it by using the following asset: UnitySafariViewController

It opens the Safari ViewController out of Unity and supports the callback which contains the authorization code. Once you've received the auth code, everything else (request for the token etc.) can be done by the Unity WebRequest. Keep in mind to add the redirect uri for the broker on iOS into the Info.plist.

Most important link: The auth flow

0

You can use the Microsoft identity platform OAuth 2.0 with Device Login. It is designed to work on input-constrained applications (like Unity apps) and devices. Here you can find the documentation for it.

E.g. AltspaceVR (a Unity app) is using that for its Mac and Quest2 versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.