public async Task <IActionResult> CreateToken([FromBody] LoginViewModel model) { if (ModelState.IsValid) { var user = await _userManager.FindByNameAsync(model.Username); if (user != null) { var result = await _signInManager.CheckPasswordSignInAsync(user, model.Password, false); if (result.Succeeded) { var principal = await _claimsFactory.CreateAsync(user); // Create the token var claims = principal.Claims.ToList(); claims.Add(new Claim(JwtRegisteredClaimNames.Sub, user.Email)); claims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())); claims.Add(new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName)); foreach (var role in await _userManager.GetRolesAsync(user)) { claims.Add(new Claim(ClaimTypes.Role, role)); } var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Tokens:Key"])); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken( _config["Tokens:Issuer"], _config["Tokens:Audience"], claims.ToArray(), expires: DateTime.Now.AddMinutes(30), signingCredentials: creds); var results = new { token = new JwtSecurityTokenHandler().WriteToken(token), expiration = token.ValidTo , isTeacher = token.Claims.Where(c => c.Type == "Teacher").Select(c => c.Value) }; await _programRepository.AddTokenUser(results.token, user.Id); return(Created("", results)); } } } return(BadRequest()); }