Search code examples
c#asp.net-coreasp.net-core-identity

Show the user a message when you enter the password incorrectly ASP.NET Core 3.0 MVC


what happens is that when the user enters the password incorrectly, the following message appears:

Invalid login attempt.

But this message also appears when the user has not validated his email. How can I do to show the user the message for each situation?

if (ModelState.IsValid)
{
    // This doesn't count login failures towards account lockout
    // To enable password failures to trigger account lockout, set lockoutOnFailure: true
    var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
    if (result.Succeeded)
    {
        _logger.LogInformation("User logged in.");
        return LocalRedirect(returnUrl);
    }

    if (result.RequiresTwoFactor)
    {
        return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
    }

    if (result.IsLockedOut)
    {
        _logger.LogWarning("User account locked out.");
        return RedirectToPage("./Lockout");
    }
    else
    {
        ModelState.AddModelError(string.Empty, "Invalid login attempt.");
        return Page();
    }
}

Solution

  • For PasswordSignInAsync, it checks the username and password by UserManager

        public virtual async Task<SignInResult> PasswordSignInAsync(string userName, string password,
            bool isPersistent, bool lockoutOnFailure)
        {
            var user = await UserManager.FindByNameAsync(userName);
            if (user == null)
            {
                return SignInResult.Failed;
            }
    
            return await PasswordSignInAsync(user, password, isPersistent, lockoutOnFailure);
        }
    

    You could add these logic to identify whether username or password is invalid.

        public async Task<IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
    
            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(LoginInput.Email, LoginInput.Password, LoginInput.RememberMe, lockoutOnFailure: true);
                if (result.Succeeded)
                {
                    _logger.LogInformation("User logged in.");
                    return LocalRedirect(returnUrl);
                }
                if (result.RequiresTwoFactor)
                {
                    return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = LoginInput.RememberMe });
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return RedirectToPage("./Lockout");
                }
                else
                {
                    var user = await _userManager.FindByNameAsync(LoginInput.Email);
                    if (user == null)
                    {
                        ModelState.AddModelError(string.Empty, "Invalid UserName.");
                    }
                    else if (!await _userManager.CheckPasswordAsync(user, LoginInput.Password))
                    {
                        ModelState.AddModelError(string.Empty, "Invalid Password.");
                    }
                    return Page();
                }
            }
    
            // If we got this far, something failed, redisplay form
            return Page();
        }