【问题标题】:Fluent validation Db password with user input使用用户输入流利地验证 Db 密码
【发布时间】:2021-09-29 15:26:32
【问题描述】:

我想知道如何才能正确运行我的代码。 我想根据存储在数据库中的密码验证用户在表单中输入的 oldPassword。如果不同会引发错误,它们是否相同? 到目前为止,我有这个,但我有错误,不太确定如何修复它。 我有函数 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;
}

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

【问题讨论】:

  • 您通常会进行实际的密码检查。因为没有人将明文密码存储在数据库中,所以您只能检查用户输入的任何内容是否通过了密码检查(包括加盐、散列、完整的蒙蒂)。
  • 顺便说一句:return _userManager.CheckPasswordAsync(user.Result, oldPassword).Result; - 不要这样做。如果你有一个异步 API,就一直异步。另见:docs.fluentvalidation.net/en/latest/async.html
  • 我不存储纯文本,我只是想验证输入的旧密码是否正常。我该怎么做。谢谢
  • 执行任何您的登录操作来验证该数据库条目的密码。

标签: c# asp.net validation fluentvalidation


【解决方案1】:

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

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


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

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2015-11-22
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-10
  • 2014-02-12
相关资源
最近更新 更多