[英]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.