public async Task <IActionResult> CreateToken([FromBody] LoginModel model) { var user = await _userService.GetByEmail(model.Email); if (user != null) { var result = await _accountService.ValidatePasswordAsync(user, model.Password); if (result.Succeeded) { Claim[] claims = new[] { new Claim(JwtRegisteredClaimNames.Sub, user.Email), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()) }; SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Tokens:Key"])); SigningCredentials credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); JwtSecurityToken token = new JwtSecurityToken( _configuration["Tokens:Issuer"], _configuration["Tokens:Audience"], claims, expires: DateTime.UtcNow.AddDays(99), signingCredentials: credentials); var results = new { token = new JwtSecurityTokenHandler().WriteToken(token), expiration = token.ValidTo, user }; return(Ok(results)); } } return(BadRequest()); }
public async Task <SignInResult> LoginAsync(LoginModel model) { var user = await _userService.GetByEmail(model.Email); return(await _signInManager.PasswordSignInAsync(user.Name, model.Password, model.RememberMe, false)); }