Exemple #1
0
        private async Task <string> GenerateToken(LoginRequest request, User user, ICollection <Claim> userClaims)
        {
            var claims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.Sub, request.UserName),
                new Claim("username", request.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, await _jwtOptions.JtiGenerator()),
                new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64)
            };

            if (user.UserType > 0)
            {
                claims.Add(new Claim(_jwtOptions.CommonClaimName, user.UserType.ToString()));
            }

            claims.AddRange(userClaims.Select(c => new Claim(JwtOptions.ClaimAcessName, c.Type)));

            // Create the JWT security token and encode it.
            var jwt = new JwtSecurityToken(
                issuer: _jwtOptions.Issuer,
                audience: _jwtOptions.Audience,
                claims: claims,
                notBefore: _jwtOptions.NotBefore,
                expires: _jwtOptions.Expiration,
                signingCredentials: _jwtOptions.SigningCredentials);

            var    tokenHandler = new JwtSecurityTokenHandler();
            string encodedJwt   = tokenHandler.WriteToken(jwt);

            _tokenCache.Add(jwt.Id);

            _logger.LogInformation(LoggingEvents.GenerateItems, "Generate the Token.");

            return(encodedJwt);
        }
        public async Task <UserProfile> LoginAsync(LoginRequest request)
        {
            User user = await TryLoginAsync(request);

            var claims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.Sub, request.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, await _jwtOptions.JtiGenerator()),
                new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64)
            };

            // add database name claim
            claims.Add(new Claim(_jwtOptions.InstanceClaimName, user.InstanceName));

            // add user claims
            ICollection <Claim> userClaims = await _userManager.GetClaimsAsync(user);

            claims.AddRange(userClaims);

            // Create the JWT security token and encode it.
            var jwt = new JwtSecurityToken(
                issuer: _jwtOptions.Issuer,
                audience: _jwtOptions.Audience,
                claims: claims,
                notBefore: _jwtOptions.NotBefore,
                expires: _jwtOptions.Expiration,
                signingCredentials: _jwtOptions.SigningCredentials);

            var    tokenHandler = new JwtSecurityTokenHandler();
            string encodedJwt   = tokenHandler.WriteToken(jwt);

            _tokenCache.Add(jwt.Id);

            // Serialize and return the response
            var response = new UserProfile
            {
                UserName            = user.UserName,
                InstanceName        = user.InstanceName,
                Access              = userClaims.Select(c => c.Value).ToList(),
                Token               = encodedJwt,
                TokenExpirationDate = DateTime.Now.AddSeconds((int)_jwtOptions.ValidFor.TotalSeconds)
            };

            _logger.LogInformation("User logged in.");
            return(response);
        }