public async Task <IActionResult> LogIn(CredentialsViewModel credentials) { _log.LogInfo("Set credentials for authorization."); if (!ModelState.IsValid) { _log.LogError("Incorrect format of input."); return(BadRequest(ModelState)); } _authorizationManager = new AuthorizationManager(_appDbContext); _log.LogInfo("Check the user."); Task <System.Security.Claims.ClaimsIdentity> identity = _authorizationManager.GetClaimsIdentity(credentials.UserLogin, credentials.Password); if (identity.Result == null) { _log.LogError("Invalid username or password."); return(BadRequest(Errors.AddErrorToModelState("loginFailure", "Invalid username or password.", ModelState))); } if (await _appDbContext.IsConfirmed(credentials.UserLogin)) { string jwt = await _appDbContext.IsAuthorized(credentials.UserLogin); if (jwt == null) { _log.LogInfo("Set an access token."); jwt = await GetToken(credentials.UserLogin); } else { _log.LogInfo("User is already authorized. Get an access token from current session."); } _log.LogInfo("Successful authorize."); var result = new { token = jwt }; return(Ok(result)); } else { return(BadRequest("Please, first confirm you email, then login.")); } }