예제 #1
0
        /// <summary>
        /// Generates the json web token.
        /// </summary>
        /// <param name="user">UserDTO</param>
        /// <returns>A JwtSecurityToken object.</returns>
        private async Task <JwtSecurityToken> GetJwtSecurityToken(User user, GrantType grantType)
        {
            var expiresAfterHours          = _jwtTokenSettings.ExpiresAfterHours;
            IEnumerable <Claim> userClaims = new List <Claim>();
            var principal = await _signInManager.CreateUserPrincipalAsync(user);

            switch (grantType)
            {
            case GrantType.Password:
                userClaims = principal.Claims.Union(GetUserClaims(user)).Distinct();
                break;

            case GrantType.Tenant:
                userClaims = principal.Claims.Union(GetTenantClaims(user)).Distinct();
                //expiresAfterHours = 24;
                break;
            }

            var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtTokenSettings.SecurityKey));
            var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
            var token       = new JwtSecurityToken(
                issuer: _jwtTokenSettings.SiteAddress,
                audience: _jwtTokenSettings.Audience,
                claims: userClaims,
                expires: DateTime.UtcNow.AddHours(expiresAfterHours),
                signingCredentials: credentials
                );

            return(token);
        }