Exemple #1
0
        public JwtSecurityToken Create(IEnumerable <Claim> claims, SharedAuthOptions options)
        {
            var notBefore   = DateTime.Now;
            var expires     = notBefore.Add(TimeSpan.FromMinutes(SharedAuthOptions.LifeTimeInMinutes));
            var securityKey = SharedAuthOptions.GenerateSymmetricSecurityKey();

            claims = (
                from claim in claims
                join type in claims.Select(c => c.Type) on claim.Type equals type
                where
                type != IssuerKey &&
                type != IssuedAtKey &&
                type != ExpiresKey &&
                type != NotBeforeKey &&
                type != AudienceKey
                select claim)
                     .ToList();

            var token = new JwtSecurityToken(
                SharedAuthOptions.Issuer,
                SharedAuthOptions.Audience,
                claims,
                notBefore,
                expires,
                new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256)
                );

            return(token);
        }
Exemple #2
0
        public string EncodeJwt(IEnumerable <Claim> claims, SharedAuthOptions options)
        {
            var token      = this.jwtTokenFactory.Create(claims, options);
            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(token);

            return(encodedJwt);
        }