Пример #1
0
        public string CreateToken(UserEntity user)
        {
            var secret = Encoding.UTF8.GetBytes(Config.Secret);
            var claims = null != ClaimsGenerator?ClaimsGenerator.Invoke(user) : CreateDefaultClaims(user);

            var descriptor = new SecurityTokenDescriptor
            {
                Issuer    = null,
                Audience  = null,
                IssuedAt  = DateTime.UtcNow,
                NotBefore = DateTime.UtcNow,
                Subject   = new ClaimsIdentity(claims),
                Expires   = DateTime.UtcNow.AddSeconds(
                    Convert.ToDouble(Config.Ttl)
                    ),
                SigningCredentials =
                    new SigningCredentials(new SymmetricSecurityKey(secret), SecurityAlgorithms.HmacSha256Signature)
            };


            return(TokenHandler.WriteToken(TokenHandler.CreateJwtSecurityToken(descriptor)));
        }
Пример #2
0
        private string GenerateToken(Guid Id, string email)
        {
            SecurityTokenDescriptor TokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(JwtRegisteredClaimNames.UniqueName, Id.ToString()),
                    new Claim(JwtRegisteredClaimNames.Email, email)
                }),
                Expires            = DateTime.UtcNow.AddMonths(1),
                SigningCredentials = new SigningCredentials(
                    new SymmetricSecurityKey(
                        Encoding.ASCII.GetBytes(key)),
                    SecurityAlgorithms.HmacSha256Signature
                    ),
                Audience = "PB8",
                Issuer   = "PB8"
            };
            JwtSecurityToken Token = TokenHandler.CreateJwtSecurityToken(TokenDescriptor);

            TokenHandler.WriteToken(Token);
            return(Token.RawData);
        }