public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null) { returnUrl ??= Url.Content("~/"); ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var result = await _signInManager.PasswordSignInAsync(model.UserNameOrEmail, model.Password, model.RememberMe, lockoutOnFailure : true); // Tìm UserName theo Email, đăng nhập lại if ((!result.Succeeded) && AppUtilities.IsValidEmail(model.UserNameOrEmail)) { var user = await _userManager.FindByEmailAsync(model.UserNameOrEmail); if (user != null) { result = await _signInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, lockoutOnFailure : true); } } if (result.Succeeded) { _logger.LogInformation(1, "User logged in."); return(LocalRedirect(returnUrl)); } if (result.RequiresTwoFactor) { return(RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe })); } if (result.IsLockedOut) { _logger.LogWarning(2, "Tài khoản bị khóa"); return(View("Lockout")); } else { ModelState.AddModelError("Không đăng nhập được."); return(View(model)); } } return(View(model)); }