public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var account = _userManager.FindByNameAsync(model.Email).GetAwaiter().GetResult(); if (account == null) { ModelState.AddModelError(string.Empty, "Account doesnt exist."); return(View(model)); } //custom check if account exist in current country/region if (_userRoleCountryService.Exists(account.Id, CountryId)) { // 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(model.Email, model.Password, model.RememberMe, lockoutOnFailure : false); if (result.Succeeded) { _logger.LogInformation("User logged in."); return(RedirectToLocal(returnUrl)); } if (result.RequiresTwoFactor) { return(RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe })); } if (result.IsLockedOut) { _logger.LogWarning("User account locked out."); return(RedirectToAction(nameof(Lockout))); } else { ModelState.AddModelError(string.Empty, "Invalid login attempt."); return(View(model)); } } else { ModelState.AddModelError(string.Empty, "This login is invalid in this country."); return(View(model)); } } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> Login([FromBody] LoginViewModel model) { try { var account = await _userManager.FindByNameAsync(model.Email); if (account == null) { return(BadRequest("Account doesnt exist")); } if (_userRoleCountryService.Exists(account.Id, model.CountryId)) { return(Ok(SignIn(account, model.CountryId))); } } catch (Exception ex) { return(BadRequest(ex.Message)); } return(BadRequest()); }