我已经成功为我正在测试的租户设置了正确的注册/登录策略。我将“重置密码”属性设置为允许任何人使用他们的电子邮件重置密码。目前,用户使用他们的电子邮件(也是用户名),名字和姓氏进行注册。
但是,当我在登录页面上点击“我忘记了密码”链接时,B2C仅会将我重定向回同一页面。
如何解决这个问题。
我已经成功为我正在测试的租户设置了正确的注册/登录策略。我将“重置密码”属性设置为允许任何人使用他们的电子邮件重置密码。目前,用户使用他们的电子邮件(也是用户名),名字和姓氏进行注册。
但是,当我在登录页面上点击“我忘记了密码”链接时,B2C仅会将我重定向回同一页面。
如何解决这个问题。
Azure AD B2C 有两种不同的密码重置机制:
登录策略:应用程序无需进行额外工作,单击“我忘记了密码”会自动将用户重定向到通用的 Microsoft 品牌密码重置页面。
注册/登录策略:这需要应用程序进行一些额外工作。单击“我忘记了密码”将用户带回到应用程序,并显示错误代码。应用程序需要检测请求中的错误代码,然后进一步将用户重定向到 Azure AD B2C 密码重置策略。可以对密码重置策略进行广泛的自定义设置。
为了更详细地介绍如何实现第二种方法,以下是从B2C 注册/登录快速入门,Startup.Auth.cs中连接到身份验证失败通知并重定向到您自己的 PasswordReset 控制器操作的代码:
private Task AuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
{
notification.HandleResponse();
if (notification.ProtocolMessage.ErrorDescription != null && notification.ProtocolMessage.ErrorDescription.Contains("AADB2C90118"))
{
// If the user clicked the reset password link, redirect to the reset password route
notification.Response.Redirect("/Account/ResetPassword");
}
else if (notification.Exception.Message == "access_denied")
{
// If the user canceled the sign in, redirect back to the home page
notification.Response.Redirect("/");
}
else
{
notification.Response.Redirect("/Home/Error?message=" + notification.Exception.Message);
}
return Task.FromResult(0);
}
这里是密码重置控制器动作的代码,它将用户重定向到密码重置B2C策略中,来自同一B2C注册/登录快速入门,账户控制器
public void ResetPassword()
{
if (!Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = "/" }, Startup.PasswordResetPolicyId);
}
}
为了完整起见,请确保查看Azure AD B2C注册/登录策略设置的全面指南
我一直遇到同样的问题,想出了一个JavaScript解决方法,可以将“忘记密码”链接直接指向“重置密码”策略,而无需更改所连接的Web应用程序(Web /移动/任何其他)中的代码。
1. 我假设您有三个标准策略,如下面的截屏所示:
2. 转到“注册和登录策略”,并启用Javascript
a. 点击“注册和登录策略” -> 属性
b. 根据以下屏幕截图启用JavaScript
3. 在Microsoft文章之后,它将指导您如何为登录/注册页面创建自定义UI
a. 下载登录/注册的现成模板(Ocean Blue)(顺便说一句,它看起来比内置的经典旧版要好得多;您还可以更改背景和标志)
https://github.com/Azure-Samples/Azure-AD-B2C-page-templates/tree/master/ocean_blue
在那里,你也会找到其他模板。
b. 将这个文件夹作为整个上传到任何托管或Azure Blob 存储。
c. 只需确保为您的托管或 Azure 启用 CORS (通过 Azure Storage Explorer 轻松实现)。
4. 编写所需的 Javascript。
Add the required Javascript; On link clicked, we are taking the current url which is the signsignup, replace the policy name by the reset policy, make sure you put here your policy names (not mine), or leave it as the instructions if you are using the same policy names
<script>
$(function() {
console.log( "ready!" );
//Change Forget Password Text
$('#forgotPassword').html('Reset My Password');
//Handle Forget password click (fixing ADB2C error)
$( "#forgotPassword" ).click(function(e) {
e.preventDefault();
var oldUrl = window.location.href;
var newUrl = oldUrl.replace('B2C_1_signupsignin1','B2C_1_passwordreset1');
window.location.href = newUrl;
});
});
注意:如果我漏掉了任何步骤,请让我知道,我尽可能详细地阐述。
不要在Azure门户中选择B2C实例的“推荐”用户流程,您也可以尝试使用“标准”用户流程 - 尽管它看起来不如前者那么好看。