首页 > 解决方案 > 如何使用 Azure ADB2C 实现移动应用 AuthN/AuthZ?

问题描述

当前的 Azure ADB2C 移动应用示例在此处强制打开应用外部的浏览器组件,并在登录后重定向回应用。

有没有办法完全跳过这个丑陋而笨重的登录页面,直接从移动应用程序组件进行登录/注册?我想创建自己的登录,Activity所以我只去门户网站通过 REST 获取令牌,uri而不必在我的应用程序之外打开浏览器。

标签: androidazure-ad-b2c

解决方案


这是一个老问题,但一年后这个问题仍然引起了我的共鸣。对此有一个答案:

.WithUseEmbeddedWebView(true)

完整的代码片段(取自 Azure GitHub 示例并改编自)是:

async void OnLoginButtonClicked(object sender, EventArgs e)
    {
        AuthenticationResult result;
        try
        {
            //bool useEmbeddedWebView = !App.IsSystemWebViewAvailable;
            result = await App.AuthenticationClient
                .AcquireTokenInteractive(Constants.Scopes)
                .WithPrompt(Prompt.SelectAccount)
                .WithParentActivityOrWindow(App.UIParent)
                .WithUseEmbeddedWebView(true)
                .ExecuteAsync();

            await Navigation.PushAsync(new LogoutPage(result));
        }
        catch (MsalException ex)
        {
            if (ex.Message != null && ex.Message.Contains("AADB2C90118"))
            {
                result = await OnForgotPassword();
                await Navigation.PushAsync(new LogoutPage(result));
            }
            else if (ex.ErrorCode != "authentication_canceled")
            {
                await DisplayAlert("An error has occurred", "Exception message: " + ex.Message, "Dismiss");
            }
        }
    }

请注意,这不是一个完整的工作解决方案,但它确实强制 B2C 模板加载到 WebView 中,而不是打开浏览器并显示您的域名。把它放在共享代码项目中。

您不能使用自己的登录页面,但它至少看起来是您应用程序的一部分。

参考:Xamarin 注意事项

希望微软将在未来的版本中解决这个问题。


推荐阅读