我创建了一个 Xamarin.Forms 应用,它使用 MSAL 对 Azure B2C 目录中的用户进行身份验证。我已经按照以下示例设置了所有内容:
https://developer.xamarin.com/guides/xamarin-forms/cloud-services/authentication/azure-ad-b2c/
我在 MainActivity
和 AppDelegate
中指定了我的 PlatformParameters
。然后在我的 HomePage
中调用 AquireTokenAsync()
方法,如下所示:
AuthenticationResult ar = await App.AuthenticationClient.AcquireTokenAsync(Config.Scopes, string.Empty,
UiOptions.SelectAccount, string.Empty, null,
Config.Authority, Config.SignUpSignInpolicy);
此代码在 Android 中运行良好,并正确显示我的用户能够成功登录的 WebView
。
但是,当我在 iPhone 模拟器(iPhone 7 Plus - iOS 10.2)上执行此代码时,应用程序崩溃并抛出 MsalException 并显示以下消息:
authentication_ui_failed: The browser based authentication dialog failed to complete
这是堆栈跟踪的其余部分:
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]
我发现了另一个类似的问题here其中表示我需要向我的应用程序添加一些功能,但我找不到如何为 Xamarin.iOS 应用程序执行此操作。
最佳答案
我终于发现了我的愚蠢错误,但如果其他人遇到同样的问题,这就是导致我痛苦的原因:
我正在页面的构造函数中调用 AquireTokenAsync()
方法,因此它尚未完成加载。显然,这对于 Android 来说不是问题,但为了在 iOS 中弹出网页,需要先加载该页面。我将 AquireTokenAsync
调用移至 OnAppearing()
方法,现在它可以工作了!
关于c# - Xamarin.iOS 使用 MSALauthentication_ui_failed : The browser based authentication dialog failed to complete,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42938052/