Beispiel #1
0
        public async Task <IActionResult> Login(ApplicationUserLoginDTO model)
        {
            // ubicar usuario por su correo
            var user = await _userManager.FindByEmailAsync(model.Email);

            var validate = await _signInManager.CheckPasswordSignInAsync(user, model.Password, false);

            if (validate.Succeeded)
            {
                return(Ok(await GenerateToken(user)));
            }

            else
            {
                return(BadRequest("Acceso no valido"));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Sets JWT Token
        /// </summary>
        /// <param name="model"></param>
        private void SetToken(ApplicationUserLoginDTO model)
        {
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, model.Id.ToString()),
                new Claim(ClaimTypes.Name, model.FullName.ToString())
            };

            var key         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration.GetSection("AppSettings:Secret").Value));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(0.5),
                SigningCredentials = credentials
            };

            var tokenHandler = new JwtSecurityTokenHandler();

            var token = tokenHandler.CreateToken(tokenDescriptor);

            model.Token = tokenHandler.WriteToken(token);
        }