public async Task <IActionResult> Post(UserToLoginDto userToLogin) { try { var user = await _service.CreateSession(userToLogin); if (user == null) { //Unauthorized return(StatusCode(401, "Bad login")); } var claims = new[] { new Claim(ClaimTypes.NameIdentifier, user.ID.ToString()), new Claim(ClaimTypes.Email, user.Email) }; var key = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(_config.GetSection("Appsettings:Token").Value)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = DateTime.Now.AddDays(1), SigningCredentials = creds }; var tokenhandler = new JwtSecurityTokenHandler(); var token = tokenhandler.CreateToken(tokenDescriptor); //Ok return(StatusCode(200, new { token = tokenhandler.WriteToken(token) })); } catch (Exception e) { return(StatusCode(500, e.Message)); } }