public IActionResult Tokens([FromBody] TokensRequest tokensRequest) { var identity = GetIdentity(tokensRequest.Username, tokensRequest.Password); if (identity == null) { return(Problem("Invalid username or password.")); } var tokens = _jwtManager.GenerateTokens(tokensRequest.Username, identity.Claims, DateTime.UtcNow); return(Json(tokens)); }
public async Task <IActionResult> AuthenticateAsync(string login, string password) { var user = await userRepository.GetAsync(login); if (user == null || !BCrypt.Net.BCrypt.Verify(password, user.PasswordHash)) { return(new JsonResult(new ExceptionDto { Message = "Invalid credentials" }) { StatusCode = 422 }); } var tokens = jwtManager.GenerateTokens(login, await GetRoleAsync(login), DateTime.Now); var handler = new JwtSecurityTokenHandler(); var refreshData = handler.ReadJwtToken(tokens.RefreshToken); var date = refreshData.ValidTo; await refreshTokenRepository.Add(new RefreshToken { Token = tokens.RefreshToken, ValidTill = date }); return(new JsonResult(tokens)); }