忘记密码AspNetBoilerPlate的正确实现
Correct Implementation of Forgot Password AspNetBoilerPlate
我正在使用 aspnetboilerplate (MVC) 并希望实现忘记密码功能,以允许用户使用登录屏幕上的 link 重置自己的密码。
我想这是通过生成一个密码重置代码来工作的,然后通过电子邮件将其发送给 user.The 用户,然后跟随 link 并被带到一个允许他们重置密码的屏幕。
我卡在了初级阶段。在注意到尝试登录用户对象时,我开始使用登录操作的副本 returned。从这里我尝试设置密码重置代码。
[HttpPost]
[UnitOfWork]
public virtual async Task<JsonResult> ForgotPassword(ForgotPasswordViewModel forgotPasswordModel, string returnUrl = "", string returnUrlHash = "")
{
returnUrl = NormalizeReturnUrl(returnUrl);
if (!string.IsNullOrWhiteSpace(returnUrlHash))
{
returnUrl = returnUrl + returnUrlHash;
}
var loginResult = await _logInManager.LoginAsync(forgotPasswordModel.UsernameOrEmailAddress, "ForgotPassword", GetTenancyNameOrNull());
loginResult.User.SetNewPasswordResetCode();
switch (loginResult.Result)
{
case AbpLoginResultType.Success:
return Json(loginResult);
default:
throw _abpLoginResultTypeHelper.CreateExceptionForFailedLoginAttempt(loginResult.Result, forgotPasswordModel.UsernameOrEmailAddress, GetTenancyNameOrNull());
}
}
在
之后检查 AbpUser table
loginResult.User.SetNewPasswordResetCode();
我看不到用户的任何密码重置代码,它们都是空的。
有人能给我指出正确的方向吗?
提前致谢
感谢下面的回答是正确的,下面的完成正是有效的。明显忽略最后的json return
public virtual async Task ForgotPassword(ForgotPasswordViewModel forgotPasswordModel, 字符串 returnUrl = "", 字符串 returnUrlHash = "")
{
//var user = await GetUserByChecking(emailAddress);
var user = await _userManager.FindByEmailAsync(forgotPasswordModel.UsernameOrEmailAddress);
if (user == null)
{
throw new UserFriendlyException("User not found!");
}
user.SetNewPasswordResetCode();
//Send an email to user with the below password reset code
/* Uri.EscapeDataString(user.PasswordResetCode) */
return Json("");
}
public class AccountAppService: IAccountAppService
{
public UserManager UserManager {get; set; }
public async Task SendPasswordResetCode(string emailAddress)
{
var user = await UserManager.FindByEmailAsync(emailAddress);
if (user == null)
{
throw new UserFriendlyException("User not found!");
}
user.SetNewPasswordResetCode();
//Send an email to user with the below password reset code
/* Uri.EscapeDataString(user.PasswordResetCode) */
}
}
我正在使用 aspnetboilerplate (MVC) 并希望实现忘记密码功能,以允许用户使用登录屏幕上的 link 重置自己的密码。
我想这是通过生成一个密码重置代码来工作的,然后通过电子邮件将其发送给 user.The 用户,然后跟随 link 并被带到一个允许他们重置密码的屏幕。
我卡在了初级阶段。在注意到尝试登录用户对象时,我开始使用登录操作的副本 returned。从这里我尝试设置密码重置代码。
[HttpPost]
[UnitOfWork]
public virtual async Task<JsonResult> ForgotPassword(ForgotPasswordViewModel forgotPasswordModel, string returnUrl = "", string returnUrlHash = "")
{
returnUrl = NormalizeReturnUrl(returnUrl);
if (!string.IsNullOrWhiteSpace(returnUrlHash))
{
returnUrl = returnUrl + returnUrlHash;
}
var loginResult = await _logInManager.LoginAsync(forgotPasswordModel.UsernameOrEmailAddress, "ForgotPassword", GetTenancyNameOrNull());
loginResult.User.SetNewPasswordResetCode();
switch (loginResult.Result)
{
case AbpLoginResultType.Success:
return Json(loginResult);
default:
throw _abpLoginResultTypeHelper.CreateExceptionForFailedLoginAttempt(loginResult.Result, forgotPasswordModel.UsernameOrEmailAddress, GetTenancyNameOrNull());
}
}
在
之后检查 AbpUser tableloginResult.User.SetNewPasswordResetCode();
我看不到用户的任何密码重置代码,它们都是空的。
有人能给我指出正确的方向吗?
提前致谢
感谢下面的回答是正确的,下面的完成正是有效的。明显忽略最后的json return
public virtual async Task ForgotPassword(ForgotPasswordViewModel forgotPasswordModel, 字符串 returnUrl = "", 字符串 returnUrlHash = "") { //var user = await GetUserByChecking(emailAddress);
var user = await _userManager.FindByEmailAsync(forgotPasswordModel.UsernameOrEmailAddress);
if (user == null)
{
throw new UserFriendlyException("User not found!");
}
user.SetNewPasswordResetCode();
//Send an email to user with the below password reset code
/* Uri.EscapeDataString(user.PasswordResetCode) */
return Json("");
}
public class AccountAppService: IAccountAppService
{
public UserManager UserManager {get; set; }
public async Task SendPasswordResetCode(string emailAddress)
{
var user = await UserManager.FindByEmailAsync(emailAddress);
if (user == null)
{
throw new UserFriendlyException("User not found!");
}
user.SetNewPasswordResetCode();
//Send an email to user with the below password reset code
/* Uri.EscapeDataString(user.PasswordResetCode) */
}
}