Beispiel #1
0
        public (string token, DateTime expiresAt) GenerateJwtToken(User user)
        {
            var claims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
            };

            claims.AddRange(_claimsManager.GetUserClaims(user));

            var key       = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_settings.JwtKey));
            var creds     = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var expiresAt = DateTime.UtcNow.Date.AddDays(1).AddHours(1);

            var token = new JwtSecurityToken(
                _settings.JwtIssuer,
                _settings.JwtIssuer,
                claims,
                expires: expiresAt,
                signingCredentials: creds
                );

            var tokenHandler = new JwtSecurityTokenHandler();

            return(tokenHandler.WriteToken(token), expiresAt);
        }