public async Task <Result <AuthenticationTokens> > GenerateAuthenticationTokensAsync(Customer client) { #region Access token generation Result <string> jwtCreationResult = await accessTokenService.GenerateTokenAsync(client); if (!jwtCreationResult.Succeeded) { return(Result <AuthenticationTokens> .Failure(jwtCreationResult.Errors)); } // retrieve access token string accessToken = jwtCreationResult.Response; #endregion #region Refresh token generation Result <string> refreshTokenGenerationResult = await refreshTokenService.GenerateTokenAsync(client); if (!refreshTokenGenerationResult.Succeeded) { return(Result <AuthenticationTokens> .Failure(refreshTokenGenerationResult.Errors)); } // retrieve refreshtoken to separate variable string refreshToken = refreshTokenGenerationResult.Response; #endregion var response = new AuthenticationTokens(accessToken, refreshToken); return(Result <AuthenticationTokens> .Success(response)); }
//[ValidateAntiForgeryToken] public async Task <IActionResult> Login(UserLoginModel model) { var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, lockoutOnFailure : true); if (result == SignInResult.Failed) { return(BadRequest(new { Message = "Email or password is incorrect." })); } if (result == SignInResult.LockedOut) { return(BadRequest(new { Message = "Account locked out." })); } if (result == SignInResult.NotAllowed) { return(BadRequest(new { Message = "Access denied." })); } var account = await _accountService.GetAccountByEmailAsync <UserAccount>(model.Email); var remoteAddress = HttpContext.Connection.RemoteIpAddress.ToString(); account.Ip = remoteAddress; account.IsOnline = true; account.LastLoginDate = DateTime.UtcNow; await _accountService.UpdateEntityAsync(account); var newToken = await _accessTokenService.GenerateTokenAsync(account); var isSuccess = await _accessTokenService.SaveTokenAsync <UserAccount>(newToken, account.Id); return(Ok(new { Message = "User log in.", AuthResult = new AuthResult { AccessToken = newToken, Key = account.User.UserKey } })); }
//[ValidateAntiForgeryToken] public async Task <IActionResult> Login([FromBody] UserLoginDto dto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { //var token = Request.GetAccessToken(); //if (string.IsNullOrEmpty(token)) return new UnauthorizedResult(); var result = await _signInManager.PasswordSignInAsync(dto.Email, dto.Password, false, lockoutOnFailure : true); if (result.Succeeded) { _logger.LogInformation($"User logged in at {DateTime.UtcNow:dddd, dd MMMM yyyy HH:mm tt}"); var remoteAddress = HttpContext.Connection.RemoteIpAddress.ToString(); var account = await _accountService.GetAccountByEmailAsync <UserAccount>(dto.Email); account.Ip = remoteAddress; account.IsOnline = true; account.LastLoginDate = DateTime.UtcNow; await _accountService.UpdateEntityAsync(account); var newToken = await _accessTokenService.GenerateTokenAsync(account.Id); var isSuccess = await _accessTokenService.SaveTokenAsync(newToken, account.Id, true); return(new OkObjectResult(newToken)); } if (result.RequiresTwoFactor) { //return RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe }); } if (result.IsLockedOut) { _logger.LogWarning("User account locked out."); //return RedirectToAction(nameof(Lockout)); } } catch (Exception ex) { _logger.LogInformation(ex.Message); return(BadRequest(ex)); } return(BadRequest("Incorrect Email or Passord.")); }
//[ValidateAntiForgeryToken] public async Task <IActionResult> Login(AdministratorLoginModel model) { var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, lockoutOnFailure : true); if (result == SignInResult.TwoFactorRequired) { //return RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe }); } if (result == SignInResult.Failed) { return(BadRequest("Email or password is incorrect.")); } if (result == SignInResult.LockedOut) { return(new UnauthorizedObjectResult("Account locked out.")); } if (result == SignInResult.NotAllowed) { return(new UnauthorizedObjectResult("Access denied.")); } var account = await _accountService.GetAccountByEmailAsync <AdministratorAccount>(model.Email); if (account.IsOnline) { return(BadRequest("Already logged in.")); } var remoteAddress = HttpContext.Connection.RemoteIpAddress.ToString(); account.Ip = remoteAddress; account.IsOnline = true; account.LastLoginDate = DateTime.UtcNow; await _accountService.UpdateEntityAsync(account); var newToken = await _accessTokenService.GenerateTokenAsync(account); var isSuccess = await _accessTokenService.SaveTokenAsync <AdministratorAccount>(newToken, account.Id); return(Ok(new { Message = "Admin log in.", newToken })); }