Asp.net mvc 即使验证失败,登录请求仍在进行中

Asp.net mvc 即使验证失败,登录请求仍在进行中,asp.net-mvc,asp.net-core,fluentvalidation,Asp.net Mvc,Asp.net Core,Fluentvalidation,我对使用ASP相当陌生,因为我的大多数web开发经验都来自过时的方法 无论如何,我使用FluentValidation来验证表单条目 LoginViewModel.cs LoginValidator.cs 这就是我在我的UserController.cs中处理登录请求的方式: [HttpPost] public async Task<IActionResult> Login(LoginViewModel login) { var user = Database.Users.F

我对使用ASP相当陌生,因为我的大多数web开发经验都来自过时的方法

无论如何,我使用FluentValidation来验证表单条目

LoginViewModel.cs

LoginValidator.cs

这就是我在我的
UserController.cs
中处理登录请求的方式:

[HttpPost]
public async Task<IActionResult> Login(LoginViewModel login)
{
    var user = Database.Users.FirstOrDefault(x => x.Username == login.Username && x.Password == login.Password);
    if (user == null)
    {
        ModelState.AddModelError("Login", "Invalid username/password combination.");
        return View();
    }
    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
    identity.AddClaim(new Claim(ClaimTypes.Name, user.Username));
    var principal = new ClaimsPrincipal(identity);
    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
    return RedirectToAction("Login");
}
[HttpPost]
公共异步任务登录(LoginViewModel登录)
{
var user=Database.Users.FirstOrDefault(x=>x.Username==login.Username&&x.Password==login.Password);
if(user==null)
{
AddModelError(“登录”,“无效的用户名/密码组合”);
返回视图();
}
var identity=newclaimsidentity(CookieAuthenticationDefaults.AuthenticationScheme,ClaimTypes.Name,ClaimTypes.Role);
identity.AddClaim(新声明(ClaimTypes.Name,user.Username));
var principal=新的ClaimsPrincipal(身份);
等待HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,主体);
返回重定向操作(“登录”);
}
但是,在提交空白登录表单时,我的最终结果是:

如果验证失败,为什么仍在提交登录请求


我是否也必须在UserController中进行验证,或者是否有一些操作不太正确?

啊,没关系。我发现使用
ModelState.IsValid
是有效的,这样我就不用在提交登录请求之前再次手动验证模型了。

啊,没关系。我发现使用
ModelState.IsValid
是有效的,这样我就不用在提交登录请求之前再次手动验证模型了。

您在
var user=Database.Users.FirstOrDefault(x=>x.Username==login.Username&&x.Password==login.Password)中得到了什么?没关系。如果验证失败,我希望它不会到达那个点。我可以在控制器中手动验证它,但是添加到服务等有什么意义呢?您在
var user=Database.Users.FirstOrDefault(x=>x.Username==login.Username&&x.Password==login.Password)中得到了什么?没关系。如果验证失败,我希望它不会到达那个点。我可以在控制器中手动验证它,但是添加到服务等有什么意义呢?
public class LoginValidator : AbstractValidator<LoginViewModel>
{
    public LoginValidator()
    {
        RuleFor(x => x.Username).NotEmpty().WithMessage("Username cannot be empty.");
        RuleFor(x => x.Password).NotEmpty().WithMessage("Password cannot be empty.");
    }
}
//Validators
services.AddSingleton<IValidator<LoginViewModel>, LoginValidator>();
//FluentValidation
services.AddControllersWithViews().AddFluentValidation(fv =>
{
     fv.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
});
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel login)
{
    var user = Database.Users.FirstOrDefault(x => x.Username == login.Username && x.Password == login.Password);
    if (user == null)
    {
        ModelState.AddModelError("Login", "Invalid username/password combination.");
        return View();
    }
    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
    identity.AddClaim(new Claim(ClaimTypes.Name, user.Username));
    var principal = new ClaimsPrincipal(identity);
    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
    return RedirectToAction("Login");
}