I would like to know what can I do to have properly running my code. I want to validate the oldPassword enter by the user in the form against the password that is stored in the db. Is they are the same is good to go if are different raise an error. So far I have this but I have errors and don't pretty sure how to fix it. I have the function IsSameAsOldPassword but i dont know how to send the parameters.
RuleSet(() => RuleFor(x => x.OldPassword)
.Must((x) => IsSameAsOldPassword(x.Id, x.OldPassword))
.WithMessage("Old password incorrect"), RuleSets.Update);
private bool IsSameAsOldPassword(Guid id, string oldPassword)
{
var user = _userManager.FindByIdAsync(id.ToString());
return _userManager.CheckPasswordAsync(user.Result, oldPassword).Result;
}
Any improvement to the code will be welcome.
CodePudding user response:
I just found a solution, hope this help someone else, we just need to remove the field. the final solution will be like this:
RuleSet(() =>
{
RuleFor(x => x)
.MustAsync((x, cancellation) => IsSameAsOldPassword(x))
.WithMessage("Old password incorrect");
}, RuleSets.Update);
private async Task<bool> IsSameAsOldPassword(UserSetInputModel userInputModel)
{
userInputModel.fieldToUse
}