简体   繁体   English

Azure AD B2C永远不会登录Xamarin Forms

[英]Azure AD B2C never logs in on Xamarin Forms

I'm using Azure AD B2C with our Xamarin Forms mobile app. 我正在将Azure AD B2C与我们的Xamarin Forms移动应用程序一起使用。 However, when testing it never actually logs me in. I sign up for a new account, enter the verification code and password when prompted. 但是,在测试时,它实际上从未使我登录。我注册了一个新帐户,在出现提示时输入验证码和密码。 When I go enter my details and try to login, it just keeps taking me back to the signin page (where I need to enter my login details....again). 当我输入我的详细信息并尝试登录时,它只会使我回到登录页面(在此我需要输入我的登录详细信息...。)。

Here are my Azure AD B2C settings. 这是我的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}";

And here's my signin / signout code. 这是我的登录/注销代码。

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");
        }
    }
}

Update Looking through the Android log I see this error each time I try to log in. I'm assuming that this error is related to my issue. 更新查看Android日志,每次尝试登录时都会看到此错误。我假设此错误与我的问题有关。

在此处输入图片说明

I needed to add the following code (as per this example ) 我需要添加以下代码(按照本示例

For Android in the MainActivity.cs file In OnActivityResult you need to add 对于Android中的MainActivity.cs文件,您需要在OnActivityResult中添加

AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);

For iOS in AppDelegate.cs you need to add 对于AppDelegate.cs中的iOS ,您需要添加

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

These changes ensure that the control goes back to MSAL once the interactive portion of the authentication flow has ended. 这些更改可确保一旦身份验证流的交互部分结束,控件便返回MSAL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure AD B2c Xamarin 表单从不静默登录 - Azure AD B2c Xamarin Form never signs in silently 更改Xamarin形式的Azure AD B2C密码的方法 - Method to Change Password for Azure AD B2C in Xamarin Forms Azure AD B2C 上 Xamarin.Forms 抛出错误 B2C '权限' Uri 应该至少有 3 个段 - Azure AD B2C on Xamarin.Forms throwing error B2C 'authority' Uri should have at least 3 segments Azure AD B2C在Xamarin.Forms iOS应用中指定多个身份提供程序 - Azure AD B2C Specify multiple Identity Providers in Xamarin.Forms iOS app Xamarin.forms 的 Azure AD B2C“Safari 无法打开页面,因为地址无效”? - Azure AD B2C for Xamarin.forms "Safari cannot open the page because the address is invalid"? 如何使用 MSAL 将现有 Azure AD B2C 集成到 Xamarin forms 应用程序中? - How to Integrate existing Azure AD B2C into a Xamarin forms app using MSAL? Xamarin Forms:使用 MSAL 的 Azure AD B2C:通过代码设置客户端 ID - Xamarin Forms: Azure AD B2C using MSAL: Set Client Id through code 如何使用Azure AD B2C身份验证开发多用户Xamarin.Forms应用 - How to develop a multi-user Xamarin.Forms app with Azure AD B2C authentication Azure AD B2C 审核日志 - 图形 API - Azure AD B2C Audit Logs - Graph API Azure AD B2C - 审核日志不反映用户身份验证 - Azure AD B2C - audit logs not reflecting user authentications
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM