使用用户输入的流畅验证数据库密码

Fluent validation Db password with user input

我想知道如何正确地 运行 我的代码。 我想根据存储在数据库中的密码验证用户在表单中输入的旧密码。他们是否相同是好的,如果不同则引发错误。 到目前为止我有这个但是我有错误并且不太确定如何解决它。 我有函数 IsSameAsOldPassword 但我不知道如何发送参数。

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;
}

欢迎对代码进行任何改进。

我刚刚找到了解决方案,希望这对其他人有帮助,我们只需要删除该字段即可。最终的解决方案将是这样的:

RuleSet(() =>
{
    RuleFor(x => x)
        .MustAsync((x, cancellation) => IsSameAsOldPassword(x))
        .WithMessage("Old password incorrect");
}, RuleSets.Update);


private async Task<bool> IsSameAsOldPassword(UserSetInputModel userInputModel)
{
    userInputModel.fieldToUse
}