예제 #1
0
        public async Task ResetPasswordAsync(GoblinIdentityResetPasswordModel model,
                                             CancellationToken cancellationToken = default)
        {
            var userEntity = await _userRepo.Get(x => x.Email == model.Email && x.EmailConfirmedTime != null)
                             .FirstOrDefaultAsync(cancellationToken).ConfigureAwait(true);

            if (userEntity == null)
            {
                throw new GoblinException(nameof(GoblinIdentityErrorCode.UserNotFound),
                                          GoblinIdentityErrorCode.UserNotFound);
            }

            if (userEntity.SetPasswordToken == model.SetPasswordToken)
            {
                if (userEntity.SetPasswordTokenExpireTime < GoblinDateTimeHelper.SystemTimeNow)
                {
                    throw new GoblinException(nameof(GoblinIdentityErrorCode.SetPasswordTokenExpired),
                                              GoblinIdentityErrorCode.SetPasswordTokenExpired);
                }
            }
            else
            {
                throw new GoblinException(nameof(GoblinIdentityErrorCode.SetPasswordTokenInCorrect),
                                          GoblinIdentityErrorCode.SetPasswordTokenInCorrect);
            }

            var changedProperties = new List <string>();

            var newPasswordHashWithOldSalt =
                PasswordHelper.HashPassword(model.NewPassword, userEntity.PasswordLastUpdatedTime);

            // If user have changed password, then update password and related information
            if (newPasswordHashWithOldSalt != userEntity.PasswordHash)
            {
                userEntity.PasswordLastUpdatedTime            =
                    userEntity.RevokeTokenGeneratedBeforeTime = GoblinDateTimeHelper.SystemTimeNow;
                changedProperties.Add(nameof(userEntity.PasswordLastUpdatedTime));
                changedProperties.Add(nameof(userEntity.RevokeTokenGeneratedBeforeTime));

                userEntity.PasswordHash =
                    PasswordHelper.HashPassword(model.NewPassword, userEntity.PasswordLastUpdatedTime);
                changedProperties.Add(nameof(userEntity.PasswordHash));
            }

            userEntity.SetPasswordToken = null;
            changedProperties.Add(nameof(userEntity.SetPasswordToken));

            userEntity.SetPasswordTokenExpireTime = null;
            changedProperties.Add(nameof(userEntity.SetPasswordTokenExpireTime));

            _userRepo.Update(userEntity, changedProperties.ToArray());

            await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);
        }
예제 #2
0
        public static async Task ResetPasswordAsync(GoblinIdentityResetPasswordModel model, CancellationToken cancellationToken = default)
        {
            ValidationHelper.Validate <GoblinIdentityResetPasswordModelValidator, GoblinIdentityResetPasswordModel>(model);

            try
            {
                var endpoint = GetRequest(model.LoggedInUserId).AppendPathSegment(GoblinIdentityEndpoints.ResetPassword);

                await endpoint
                .PutJsonAsync(model, cancellationToken : cancellationToken)
                .ConfigureAwait(true);
            }
            catch (FlurlHttpException ex)
            {
                await FlurlHttpExceptionHelper.HandleErrorAsync(ex).ConfigureAwait(true);
            }
        }
예제 #3
0
        public async Task <IActionResult> SubmitResetPassword(ResetPasswordModel model, CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.WarningMessage = Messages.InvalidData;

                return(View("ResetPassword", model));
            }

            try
            {
                var resetPasswordModel = new GoblinIdentityResetPasswordModel
                {
                    Email            = model.Email,
                    SetPasswordToken = model.SetPasswordToken,
                    NewPassword      = model.NewPassword
                };

                await GoblinIdentityHelper.ResetPasswordAsync(resetPasswordModel, cancellationToken);

                ViewBag.SuccessMessage = "Your account have updated password, now you can login with the new password.";

                return(View("Login"));
            }
            catch (GoblinException e)
            {
                ViewBag.ErrorMessage = e.ErrorModel.Message;

                return(View("ResetPassword", model));
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;

                return(View("ResetPassword", model));
            }
        }
예제 #4
0
        public async Task <IActionResult> ResetPassword([FromBody] GoblinIdentityResetPasswordModel model, CancellationToken cancellationToken = default)
        {
            await _userService.ResetPasswordAsync(model, cancellationToken);

            return(NoContent());
        }