private IActionResult BuildToken(UserInfo userInfo) { var claims = new[] { new Claim(JwtRegisteredClaimNames.UniqueName, userInfo.Email), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()) }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["LlaveSecreta"])); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var expiration = DateTime.UtcNow.AddDays(7); JwtSecurityToken token = new JwtSecurityToken( issuer: "yourdomain.com", audience: "yourdomain.com", claims: claims, expires: expiration, signingCredentials: creds); return(Ok(new { token = new JwtSecurityTokenHandler().WriteToken(token), expiration = expiration })); }
public async Task <IActionResult> Login([FromBody] UserInfo userInfo) { if (ModelState.IsValid) { var result = await _signInManager.PasswordSignInAsync(userInfo.Email, userInfo.Password, isPersistent : false, lockoutOnFailure : false); if (result.Succeeded) { return(BuildToken(userInfo)); } else { ModelState.AddModelError(string.Empty, "Invalid login attempt."); return(BadRequest(ModelState)); } } else { return(BadRequest(ModelState)); } }
public async Task <IActionResult> CreateUser([FromBody] UserInfo model) { if (ModelState.IsValid) { var user = new AplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { return(BuildToken(model)); } else { return(BadRequest(ModelState)); } } else { return(BadRequest(ModelState)); } }