Azure AD B2C永远不会登录Xamarin Forms

时间:2018-10-25 10:15:58

标签: azure xamarin.forms azure-active-directory azure-ad-b2c

我在我们的 Xamarin Forms 移动应用中使用了 Azure AD B2C 。但是,在测试时,它实际上从未使我登录。我注册了一个新帐户,在出现提示时输入验证码和密码。当我输入我的详细信息并尝试登录时,它只会继续带我回到登录页面(在该页面中,我需要再次输入登录详细信息...。)

这是我的 Azure AD B2C 设置。

public const string Tenant = "mytenant.onmicrosoft.com";
public static string ClientId = "my-clientid-for-the-application";
public static string SignUpSignInPolicy = "B2C_1_IfmMobileApp";
public static string PolicyResetPassword = "B2C_1_IfmMobileAppReset ";
public static string[] Scopes = { "" };
public static readonly string CustomRedirectUrl = $"msal{ClientId}://auth";
public static string AuthorityBase = $"https://login.microsoftonline.com/tfp/{Tenant}/";
public static string Authority = $"{AuthorityBase}{SignUpSignInPolicy}";
public static string AuthorityPasswordReset = $"{AuthorityBase}{PolicyResetPassword}";

这是我的登录/注销代码。

private async void OnSignInSignOut(object sender, EventArgs e)
{
    try
    {
        IEnumerable<IAccount> accounts = await AuthenticationService.PCA().GetAccountsAsync();

        if (btnSignInSignOut.Text == "Sign in")
        {
            var account = this.GetAccountByPolicy(accounts, ApplicationConstants.SignUpSignInPolicy);
            AuthenticationResult ar =
                        await AuthenticationService.PCA().AcquireTokenAsync(ApplicationConstants.Scopes, account, App.UiParent);
            UpdateUserInfo(ar);
            UpdateSignInState(true);
        }
        else
        {
            foreach (var user in accounts)
            {
                await AuthenticationService.PCA().RemoveAsync(user);
            }
            UpdateSignInState(false);
        }
    }
    catch (MsalClientException ex)
    {
        await DisplayAlert($"MSAL Exception:", ex.ToString(), "Dismiss");
    }
    catch (Exception ex)
    {
        // Checking the exception message 
        // should ONLY be done for B2C
        // reset and not any other error.
        if (ex.Message.Contains("AADB2C90118"))
        {
            OnPasswordReset();
        }
        else
        {
            await DisplayAlert($"Exception:", ex.ToString(), "Dismiss");
        }
    }
}

更新 浏览Android日志时,每次尝试登录时都会看到此错误。我假设此错误与我的问题有关。

enter image description here

1 个答案:

答案 0 :(得分:0)

我需要添加以下代码(按照此example

MainActivity.cs文件中的 Android ,在 OnActivityResult 中,您需要添加

AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);

对于AppDelegate.cs中的 iOS ,您需要添加

public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
    AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(url);
    return true;
}

这些更改可确保一旦身份验证流的交互部分结束,控件便返回到MSAL。