c# - Xamarin.iOS 使用 MSALauthentication_ui_failed : The browser based authentication dialog failed to complete

标签 c# azure xamarin.ios xamarin.forms azure-ad-b2c

我创建了一个 Xamarin.Forms 应用,它使用 MSAL 对 Azure B2C 目录中的用户进行身份验证。我已经按照以下示例设置了所有内容:

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

我在 MainActivityAppDelegate 中指定了我的 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/

相关文章:

c# - 在代码隐藏中使用数据绑定(bind)

c# - 内容类型 - WebAPI - 请求 header

azure - Azure 事件中心中的偏移量

ios - Monotouch - 无法在单击按钮时关闭当前 View

c# - 在 Controller 上找不到捕获操作方法

c# - 接受在一段时间内保持更改的更改值

c# - 为什么注入(inject)到我的 Azure Function v2 中的 ILogger 缺少 APPINSIGHTS_INSTRUMENTATIONKEY?

azure - 为什么这个 PhantomJS 进程会导致 "Directory <x> does not exist."错误?

iphone - 为 MonoTouch.Dialog 创建自定义 Section 类?

ios - 2010 年 10 月的 Macbook Airs 是否足以运行所有工具来开发 iPhone 应用程序?