Ejemplo n.º 1
0
        public AuthResponse GetJWTToken(string claimValue)
        {
            AuthResponse response = new AuthResponse();

            if (string.IsNullOrWhiteSpace(claimValue))
            {
                response.AddErrorMessage("Valor da claim não recebido");
                return(response);
            }

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_appSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Sid, claimValue)
                }),
                Expires            = DateTime.UtcNow.AddDays(1),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);

            response.Key = tokenHandler.WriteToken(token);

            return(response);
        }