[英]In .Net Core how to validate the password before entering in Controller Action?
这是我的 DTO 注册请求:
public class RegistrationRequestDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Compare("Password")]
public string PasswordConfirmation { get; set; }
}
当我看到“[DataType(DataType.Password)]”时,我希望框架检查密码是否遵守来自身份框架的规则。 例如,在启动时我有:
[...]
services
.AddIdentity<User, Role>(options =>
{
[...]
options.Password.RequireUppercase = true;
options.Password.RequireDigit = true;
options.Password.RequiredLength = 6;
options.Password.RequireLowercase = true;
})
[...]
所以我尝试使用“test”作为密码我应该有一个错误 400 调用 API 来注册我的新用户。 但是modelState.isValid总是等于 true。
如何查看密码?
您只需要使用以下内容装饰您的视图模型的属性:
[Remote(action: "ValidatePassword", controller: "MyValidationController", ErrorMessage = "Remote validation is working")]
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
然后在 controller(在本例中名为“MyValidationController”)中创建一个操作(在本例中名为“ValidatePassword”)并在其中添加您的逻辑:
[AcceptVerbs("Get", "Post")]
public async Task<IActionResult> ValidatePassword(string password)
{
bool isvalid = true;
//
validationLogic
//
if (isvalid)
return Json(data: true);
else
return Json(data: false);
}
参考: 远程属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.