Exemplo n.º 1
0
        public ActionResult Login(UsuarioAuthLoginDTO datosRegistro)
        {
            var usuarioCredencial = _UsuariosRepo.Login(datosRegistro.Correo, datosRegistro.Password);

            if (usuarioCredencial == null)
            {
                return(Unauthorized());
            }
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, usuarioCredencial.Correo.ToString()),
                new Claim(ClaimTypes.Name, usuarioCredencial.Correo),
            };
            var key             = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:TokenKey").Value));
            var credencial      = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);
            var descriptorToken = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(7),
                SigningCredentials = credencial,
            };

            var tokenHandle = new JwtSecurityTokenHandler();
            var token       = tokenHandle.CreateToken(descriptorToken);

            return(Ok(new
            {
                token = tokenHandle.WriteToken(token)
            }));
        }
Exemplo n.º 2
0
        public IActionResult Login(UsuarioAuthLoginDTO usuarioAuthLoginDTO)
        {
            var usuarioDesdeRepo = _userRepo.Login(usuarioAuthLoginDTO.Usuario, usuarioAuthLoginDTO.Password);

            if (usuarioDesdeRepo == null)
            {
                return(Unauthorized());
            }
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, usuarioDesdeRepo.Id.ToString()),
                new Claim(ClaimTypes.Name, usuarioDesdeRepo.Usuario.ToString())
            };

            // Creacion del Token

            var key          = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value));
            var credenciales = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = credenciales
            };
            var tokenHandler = new JwtSecurityTokenHandler();
            var token        = tokenHandler.CreateToken(tokenDescriptor);

            return(Ok(new
            {
                token = tokenHandler.WriteToken(token)
            }
                      ));
        }