Example #1
0
 internal static string CheckJWTOption(TokenGenerateOption option)
 {
     var result = "";
     result += IsSecretEmpty(option.Secret);
     result += IsSecretGreatThen16(option.Secret);
     return result;
 }
        public TokenGenerator(TokenGenerateOption options)
        {
            var errors = Utilities.CheckJWTOption(options);

            if (!String.IsNullOrEmpty(errors))
            {
                throw new ArgumentException(errors);
            }
            _options = options;
        }
        public TokenGenerator(string secret)
        {
            _options = new TokenGenerateOption()
            {
                Secret = secret
            };
            var errors = Utilities.CheckJWTOption(_options);

            if (!String.IsNullOrEmpty(errors))
            {
                throw new ArgumentException(errors);
            }
        }
        /// <summary>
        /// Create JWT token
        /// </summary>
        /// <param name="options">Generate options</param>
        /// <returns></returns>
        public string GenerateToken(List <Claim> claims)
        {
            //assign default options
            if (_options == null)
            {
                _options = new TokenGenerateOption();
            }
            _options.Subject = claims;

            var tokenDescriptor = MakeTokenDescriptor(_options);

            var tokenHandler = new JwtSecurityTokenHandler();

            var stoken = tokenHandler.CreateToken(tokenDescriptor);

            var tokenWzScheme = tokenHandler.WriteToken(stoken);

            return(tokenWzScheme);
        }
        private SecurityTokenDescriptor MakeTokenDescriptor(TokenGenerateOption options)
        {
            var result = func(options);

            return(result);
        }