public async Task<ActionResult> Login(LoginViewModel vm, string returnUrl) { if (ModelState.IsValid) { var signInResult = await _signInManager.PasswordSignInAsync( vm.Username, vm.Password, true, // it is persistent, so the login state will be remembered for some time false); // don't lock out the user if the sign in fails if (signInResult.Succeeded) { if (string.IsNullOrWhiteSpace(returnUrl)) { // green light to see the trips return RedirectToAction("Trips", "App"); } else { return Redirect(returnUrl); } } else { // error on the object level ModelState.AddModelError("", "Username or password incorrect"); } } // if failure, go back to the same Login screen again return View(); }
public async Task<ActionResult> Login (LoginViewModel vm, string returnUrl) { if (ModelState.IsValid) { var signInResult = await _signInManager.PasswordSignInAsync(vm.Username, vm.Password, true, false); if (signInResult.Succeeded) { if (string.IsNullOrWhiteSpace(returnUrl)) { return RedirectToAction("Trips", "App"); } else { return Redirect(returnUrl); } } else { ModelState.AddModelError("", "Username or password incorrect"); } } return View(); }
public async Task<IActionResult> Login(LoginViewModel vm, string returnUrl) { if (ModelState.IsValid) { // Since our sign in manager uses an async method, we must add the await // keyword in front, add async keyword to the signature and wrap the return // with a Task object. var signInResult = await _signInManager.PasswordSignInAsync(vm.Username, vm.Password, true, false); if (signInResult.Succeeded) { // When we redirect, we have a query string call ReturnUrl in the navbar. // It's passed to us with the login redirction. // We add the following If-Else because in the future if we support // the authorize attribute on different actions, this would redirect to // where we attempted to go. e.g. account page, change profile page.... if (string.IsNullOrWhiteSpace(returnUrl)) { return RedirectToAction("Trips", "App"); } else { return Redirect(returnUrl); } } else { // We're adding this model state so that when it drops down to the view // it'll get shown to the user and the user will attempt sign in again. ModelState.AddModelError("", "Username or password incorrect"); } } return View(); }