Skip to content

Xamarin Android specifics 2x

Jean-Marc Prieur edited this page Apr 12, 2019 · 3 revisions

Xamarin Android specific considerations

Setting the parent activity

Since MSAL.NET 3.0.0

bool useEmbeddedWebView = false;

var authResult = AcquireToken(scopes, parentActivity)
 .WithEmbeddedWebView(useEmbeddedWebView)
 .ExecuteAsync()
  ;

Before MSAL.NET 3.x

You need to call an overload of AcquireTokenAsync that uses UIParent, which needs to be initialized with an Activity.

// From an activity class
var uiParent = new UIParent(this, useEmbeddedWebview: true);

Ensuring control goes back to MSAL once the interactive portion of the authentication flow ends

On Android, you need to override the OnActivityResult method of the Activity and call the SetAuthenticationContinuationEventArgs method of the AuthenticationContinuationHelper MSAL class.

protected override void OnActivityResult(int requestCode, 
                                         Result resultCode, Intent data)
{
 base.OnActivityResult(requestCode, resultCode, data);
 AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode,
                                                                         resultCode,
                                                                         data);
}

That line ensures that the control goes back to MSAL once the interactive portion of the authentication flow ended.

Update the Android manifest

The AndroidManifest.xml should contain the following values:

<activity android:name="microsoft.identity.client.BrowserTabActivity">
	<intent-filter>
		<action android:name="android.intent.action.VIEW" />
		<category android:name="android.intent.category.DEFAULT" />
		<category android:name="android.intent.category.BROWSABLE" />
		<data android:scheme="msal{client_id}" android:host="auth" />
         </intent-filter>
</activity>

Sample illustrating Android specific properties

More details are provided in the Android Specific Considerations paragraph of the following sample's readme.md file:

Sample Platform Description
https://github.com/Azure-Samples/active-directory-xamarin-native-v2 Xamarin iOS, Android, UWP A simple Xamarin Forms app showcasing how to use MSAL to authenticate MSA and Azure AD via the AADD v2.0 endpoint, and access the Microsoft Graph with the resulting token.

Getting started with MSAL.NET

Acquiring tokens

Desktop/Mobile apps

Web Apps / Web APIs / daemon apps

Advanced topics

News

FAQ

Other resources

Clone this wiki locally