-
Notifications
You must be signed in to change notification settings - Fork 366
How to migrate from using Android Broker on ADAL.NET to MSAL.NET
You've been using ADAL.NET and Android broker, and it's finally time to migrate to use the next generation Microsoft authentication library, MSAL.NET, which, as of release 4.9, supports the broker on Android.
Where to start? This document will help you migrate your Xamarin Android app from ADAL to MSAL.
This document assumes that you already have a Xamarin Android app that is integrated with the Android broker. If you do not, it would be best to move directly to MSAL.NET and begin the broker implementation there. See this documentation for details on invoking the Android broker in MSAL.NET with a new application.
Brokers are applications, provided by Microsoft, on Android and iOS (Microsoft Authenticator on iOS and Android, InTune Company Portal on Android).
They enable:
- Single-Sign-On,
- Device identification, which is required by some conditional access policies (See Device management)
- Mobile device management which is often required for enterprise scenarios (See Mobile Device Management)
- Application identification verification, also required in some enterprise scenarios (See for instance Intune mobile application management, or MAM)
Current ADAL code: | MSAL counterpart: |
In ADAL.NET, broker support was enabled on a per-authentication context basis, it is disabled by default. You had to set a
public PlatformParameters(
Activity callerActivity,
bool useBroker) Also, in the platform specific code, in this example, in the page renderer for Android, set the
page.BrokerParameters = new PlatformParameters(
this,
true,
PromptBehavior.SelectAccount); Then, include the parameters in the acquire token call: AuthenticationResult result =
await
AuthContext.AcquireTokenAsync(
Resource,
ClientId,
new Uri(RedirectURI),
platformParameters)
.ConfigureAwait(false); |
In MSAL.NET, broker support is enabled on a per-Public Client Application basis. It is disabled by default. You must use the
var app = PublicClientApplicationBuilder
.Create(ClientId)
.WithBroker()
.WithReplyUri(redirectUriOnAndroid)
.Build(); In the Acquire Token call: result = await app.AcquireTokenInteractive(scopes)
.WithParentActivityOrWindow(App.RootViewController)
.ExecuteAsync(); |
In ADAL.NET, you passed in an activity(usually the MainActivity) as part of the PlatformParameters (see example in Step One). However, in MSAL.NET, to give the developer more flexibility, an activity is used, but not required in regular Android usage. However, in order to use the broker, you will need to set the activity in order to send and receive responses from broker.
Current ADAL code: | MSAL counterpart: |
The activity is passed into the PlatformParamters in the Android specific platform.
page.BrokerParameters = new PlatformParameters(
this,
true,
PromptBehavior.SelectAccount); |
In MSAL.NET, you will need to do two things to set the object window for Android:
For example: In public static object RootViewController { get; set; } In LoadApplication(new App());
App.RootViewController = this; In the Acquire Token call: result = await app.AcquireTokenInteractive(scopes)
.WithParentActivityOrWindow(App.RootViewController)
.ExecuteAsync(); |
Both ADAL and MSAL will call the broker, and broker will, in turn, call back to your application through the OpenUrl
method of the AppDelegate
class. More information available here
✔️There are no changes here between ADAL.NET and MSAL.NET
ADAL.NET and MSAL.NET use URLs to invoke the broker and return the broker response back to the app. Fortunatly, the URL schemes are the same for ADAL and MSAL so you will not need to make any changes here. More information available here
✔️There are no changes here between ADAL.NET and MSAL.NET
- 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
- 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
- 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
- High Availability
- Regional
- 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