cvanbeek
cvanbeek

Reputation: 1961

Xamarin.iOS using MSAL authentication_ui_failed: The browser based authentication dialog failed to complete

I've created a Xamarin.Forms app which uses MSAL to authenticate users in an Azure B2C directory. I've set up everything like in the following example:

https://developer.xamarin.com/guides/xamarin-forms/cloud-services/authentication/azure-ad-b2c/

I specify my PlatformParameters in both the MainActivity and AppDelegate. Then in my HomePage I call the AquireTokenAsync() method like follows:

AuthenticationResult ar = await App.AuthenticationClient.AcquireTokenAsync(Config.Scopes, string.Empty, 
                                UiOptions.SelectAccount, string.Empty, null,
                                Config.Authority, Config.SignUpSignInpolicy);

This code works fine in Android and correctly shows a WebView where my users are able to successfully login.

However, when I execute this code on the iPhone Simulator (iPhone 7 Plus - iOS 10.2) the app crashes and throws a MsalException with the message:

authentication_ui_failed: The browser based authentication dialog failed to complete

Here is the rest of the stack trace:

Authority: https://login.microsoftonline.com/<myAuthority>/
    Scope: <myClientId>
    ClientId: <MyClientId>
    CacheType: null
3/21/2017 8:41:10 PM: - WebUI.cs: System.NullReferenceException: Object reference not set to an instance of an object
 at Microsoft.Identity.Client.WebUI.Authenticate (System.Uri authorizationUri, System.Uri redirectUri, System.Collections.Generic.IDictionary`2[TKey,TValue] additionalHeaders, Microsoft.Identity.Client.Internal.CallState callState) [0x00029]
Mar 21 15:41:10 3/21/2017 8:41:10 PM: - AcquireTokenHandlerBase.cs: Microsoft.Identity.Client.MsalException: authentication_ui_failed: The browser based authentication dialog failed to complete ---> System.NullReferenceException: Object reference not set to an instance of an object
 at Microsoft.Identity.Client.WebUI.Authenticate (System.Uri authorizationUri, System.Uri redirectUri, System.Collections.Generic.IDictionary`2[TKey,TValue] additionalHeaders, Microsoft.Identity.Client.Internal.CallState callState) [0x00029]

I found another similar question here which says I need to add a few capabilities to my application, but I can't find how to do that for a Xamarin.iOS app.

Upvotes: 0

Views: 867

Answers (1)

cvanbeek
cvanbeek

Reputation: 1961

I finally figured out my dumb mistake but in case anyone else runs into the same issue here is what caused me my pain:

I was calling the AquireTokenAsync() method in the constructor of my page, so it hadn't finished loading yet. Apparently, this isn't an issue for Android, but in order for the web page to pop up in iOS, the page needs to be already loaded. I moved my AquireTokenAsync call to my OnAppearing() method and now it works!

Upvotes: 4

Related Questions