-
Notifications
You must be signed in to change notification settings - Fork 343
Unity UWP dev app
This wiki describes how to build a Unity app build for UWP that uses MSAL.NET. Also see Troubleshooting Unity wiki on related errors with Unity UWP apps.
- Create a Unity ID. (https://unity.com/)
- Via Visual Studio Installer, install Game development with Unity workload.
- Open Unity Hub, which was installed in the previous step, and sign into it.
- Install a Unity version.
- In the left side menu, select Installs.
- Click Add button.
- In the Add Unity Version window, select a Unity version (Recommended or latest LTS).
- Click Next.
- Check the following modules and click Done.
- Universal Windows Platform Build Support
- Windows Build Support (IL2CPP)
- After the installation completes, this version will show up in the Installs section.
- In Unity Hub, click on Projects in the left side menu.
- Click Add button.
- Navigate to the
..\microsoft-authentication-library-for-dotnet\tests\devapps\Unity
directory. - Click Select Folder.
-
Projects section will now show a row entry for the Unity dev app folder.
- Click on the project.
- Unity will open the project and start resolving and importing dependencies. (This may take a few minutes.)
This app creates a PublicClientApplication
, then calls AcquireTokenWithDeviceCode
and AcquireTokenSilent
. The device code and progress are printed on the screen.
In the Project tab, select Assets > Scenes. Double-click on MSALScene to load it on the center screen. Scenes are like modules or assets that contain content that is part of the app.
Pressing the play button at the top of the screen will run the app. However, this will run the app on the native framework (ex. Windows x64).
In the top left side, Hierarchy tab lists the components that are used in the app and are included in this scene. Main Camera is the default component. Canvas component has a DeviceCodeText text field and a scroll view where progress is printed.
Select Canvas component. The Inspector tab on the right will populate with the component properties. At the bottom, it shows that there's MSAL Script that's linked to this component. Scripts are behaviors that run along with this component.
MSALScript.cs
is located in ..Unity\Assets\Scripts
folder. Open Assembly-CSharp.csproj
with Visual Studio to edit the script. The script file must inherit from MonoBehavior
, that's how Unity knows to execute it. Script files can have specific methods that Unity knows to call at specific frequencies. In this case, Start
method gets called once when component is first executed and Update
gets called once per frame. The Start
method invokes the behavior to login using MSAL.
If Assembly-CSharp.csproj
was not generated, go to Edit > Preferences > External Tools. Check Embedded packages and Local packages.
MSAL dependency itself has to be placed in ..\Unity\Assets\Plugins
. It has to be a .NET Standard DLL.
..\Unity\Assets\link.xml
has an entry <assembly fullname="Microsoft.Identity.Client" preserve="all"/>
which prevents MSAL code from being stripped when Unity builds and optimizes the project.
- Go to Edit > Project Settings > Player > Universal Windows Platform settings tab.
- In Publishing Settings tab, under Capabilities, check InternetClient, InternetClientServer, and PrivateNetworkClientServer.
- Go to File > Build Settings.
- Verify Universal Windows Platform is selected. Click Switch Platform at the bottom if needed.
- Verify Minimum Platform Version is at least
10.017763.0
. Older versions will download a lot more Windows dependencies. - Verify Build configuration is
Release
. - In order to be able to debug the scripts when the app is running, the following options should be checked: Copy PDB files, Development Build, and Script Debugging.
- Click Build.
- In the dialog create an empty folder (ex. UWP) and then select it.
- Unity will now take some time to build the project (using IL2CPP plugin) and generate a C++ project in the selected folder.
- For now Unity does not automatically create a UWP app - these steps have to be done manually.
- For now Unity does not automatically create a UWP app - these steps have to be done manually.
- Open the UnityTestApp.sln C++ project with Visual Studio.
- Visual Studio might require to install some missing C++ frameworks at this point.
- Change the build configuration to
Release
andx86
orx64
. - Right-click on the UnityTestApp (Universal Windows) project in the Solution Explorer and select Deploy.
- It's advised to clean the contents of UWP folder between rebuilding the project from Unity.
- From the Start open UnityTestApp.
- Go through the device code flow.
- MSAL logs and Unity logs are written to
%AppData%\Local\Packages\UnityTestApp_pzq3xp76mxafg\TempState\UnityPlayer.log
.
- Home
- Why use MSAL.NET
- Is MSAL.NET right for me
- Scenarios
- Register your app with AAD
- Client applications
- Acquiring tokens
- MSAL samples
- Known Issues
- AcquireTokenInteractive
- WAM - the Windows broker
- .NET Core
- Maui Docs
- Custom Browser
- Applying an AAD B2C policy
- Integrated Windows Authentication for domain or AAD joined machines
- Username / Password
- Device Code Flow for devices without a Web browser
- ADFS support
- Acquiring a token for the app
- Acquiring a token on behalf of a user in Web APIs
- Acquiring a token by authorization code in Web Apps
- High Availability
- Token cache serialization
- Logging
- Exceptions in MSAL
- Provide your own Httpclient and proxy
- Extensibility Points
- Clearing the cache
- Client Credentials Multi-Tenant guidance
- Performance perspectives
- Differences between ADAL.NET and MSAL.NET Apps
- PowerShell support
- Testing apps that use MSAL
- Experimental Features
- Proof of Possession (PoP) tokens
- Using in Azure functions
- Extract info from WWW-Authenticate headers
- SPA Authorization Code