Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct Implementation of Forgot Password AspNetBoilerPlate

Im using aspnetboilerplate (MVC) and wanted to implement a forgot password feature to allow the user to reset their own passwords using a link on the login screen.

I imagine this to work by generating a password reset code which is then emailed to the user.The user follows the link and is taken to a screen allowing them to reset the password.

Im stuck at the initial stage. i started with a copy of the login action after noticing that when attempting to log in the user object was returned. From here i attempt to set a password reset code.

        [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());
            }
        }

Checking the AbpUser table after the

loginResult.User.SetNewPasswordResetCode();

i cannot see any password reset code for the user, they are all null.

Could someone point me in the right direction.

Thanks in advance

Thanks to answer below for being correct, just for completion below is exactly what worked. Obviously ignore the json return at the end

public virtual async Task<JsonResult> ForgotPassword(ForgotPasswordViewModel forgotPasswordModel, string returnUrl = "", string 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("");
}
like image 954
PowerMan2015 Avatar asked Mar 03 '23 16:03

PowerMan2015


1 Answers

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) */  
    }   
}
like image 155
Alper Ebicoglu Avatar answered May 16 '23 09:05

Alper Ebicoglu