Exemplo n.º 1
0
        /// <summary>
        /// Builds the token.
        /// </summary>
        /// <param name="config">The configuration.</param>
        /// <param name="claims">The claims.</param>
        /// <returns></returns>
        public static JwtToken BuildToken(this JwtConfig config, Dictionary <string, string> claims)
        {
            var secitiKey = config.CreateSymmetricSecurityKey();

            var allClaims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.Sub, config.Subject),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
            }
            .Union(claims.Select(item => new Claim(item.Key, item.Value)));

            var token = new JwtSecurityToken(
                issuer: config.Issuer,
                audience: config.Audience,
                claims: allClaims,
                expires: DateTime.UtcNow.AddMinutes(config.ExpireInMinutes),
                signingCredentials: new SigningCredentials(
                    secitiKey,
                    SecurityAlgorithms.HmacSha256));


            return(new JwtToken(token));
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates the symmetric security key from the secret
 /// </summary>
 /// <param name="config">The configuration.</param>
 /// <returns></returns>
 public static SymmetricSecurityKey CreateSymmetricSecurityKey(this JwtConfig config)
 {
     return(CreateSymmetricSecurityKey(config.SecurityKey));
 }