public async Task <IActionResult> Login([FromBody] LoginInputModel model) { 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(model.Email, model.Password, model.RememberLogin, lockoutOnFailure : true); if (result.Succeeded) { logger.LogInformation(1, "User logged in."); // because we are logging in through an api, we need to generate and send back a new XsrfToken return(Ok(new LoginResponseModel())); } else { logger.LogWarning(2, "User login failed."); var response = new LoginResponseModel { IsLockedOut = result.IsLockedOut, IsNotAllowed = result.IsNotAllowed, RequiresTwoFactor = result.RequiresTwoFactor, Result = ApiResult.AsError("Login Failed") }; return(BadRequest(response)); } } else { return(BadRequest(ApiModel.AsError("model validation failed (TODO add the errors)"))); } }
public async Task <IActionResult> Login([FromBody] LoginInputModel model) { // Hack to work around rc1 bug await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); if (ModelState.IsValid) { var result = await this.signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberLogin, lockoutOnFailure : true); if (result.Succeeded) { this.logger.LogInformation(1, "User logged in."); return(Ok(new LoginResponseModel { Result = ApiResult.AsSuccess() })); } else { this.logger.LogWarning(2, "User login failed."); var response = new LoginResponseModel { IsLockedOut = result.IsLockedOut, IsNotAllowed = result.IsNotAllowed, RequiresTwoFactor = result.RequiresTwoFactor, Result = ApiResult.AsError("Login Failed") }; return(BadRequest(response)); } } else { return(BadRequest(ApiModel.AsError("model validation failed (TODO add the errors)"))); } }
public async Task <IActionResult> Login([FromBody] LoginInputModel model) { 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(model.Email, model.Password, model.RememberLogin, lockoutOnFailure : false); if (result.Succeeded) { _logger.LogInformation(1, "User logged in."); return(Ok(new LoginResultModel { Result = ApiResult.AsSuccess("User logged in.") })); // return RedirectToLocal(model.ReturnUrl); } if (result.RequiresTwoFactor) { return(Ok(new LoginResultModel { RedirectTo = nameof(SendCode), ReturnUrl = model.ReturnUrl, RequiresTwoFactor = true, Result = ApiResult.AsSuccess("Second factor required.") })); // return RedirectToAction(nameof(SendCode), new { ReturnUrl = model.ReturnUrl, RememberMe = model.RememberLogin }); } if (result.IsLockedOut) { _logger.LogWarning(2, "User account locked out."); // TODO Pass ModelErrors return(BadRequest(new LoginResultModel { IsLockedOut = true, Result = ApiResult.AsError("User account locked out.") })); // return View("Lockout"); } else { ModelState.AddModelError(string.Empty, "Invalid login attempt."); return(BadRequest(new LoginResultModel { Result = ApiResult.AsError("Invalid login attempt.") })); // return View(await _account.BuildLoginViewModelAsync(model)); } } // If we got this far, something failed, redisplay form return(BadRequest(new LoginResultModel { Result = ApiResult.AsError("An error occurred.") })); // return View(await _account.BuildLoginViewModelAsync(model)); }
public static ApiResult AsException(AggregateException exception, bool includeExceptions = false) { ApiResult result; if (includeExceptions) { result = new ApiResult { Message = "Exception(s) occurred" }; result.Errors.Add("Exceptions", new List <string>(exception.InnerExceptions.Select(e => e.Message))); } else { result = ApiResult.AsError("Server Error"); } return(result); }
public static ApiResult AsException(Exception exception, bool includeExceptions = false) { ApiResult result; if (includeExceptions) { result = new ApiResult { Message = "Exception(s) occurred" }; result.Errors.Add("Exceptions", new List <string>(new[] { exception.Message })); } else { result = ApiResult.AsError("Server Error"); } return(result); }
public static ApiModel AsError(string errorMessage, int errorCode = 0) { return(new ApiModel(ApiResult.AsError(errorMessage, errorCode))); }
// Helpers public static ApiModel AsError(string errorField, string errorMessage) { return(new ApiModel(ApiResult.AsError(errorField, errorMessage))); }