Example #1
0
        public static string GenerateAccessToken(this User user, JwtSettingDto jwtSettingDto, bool isGoogleLogin = false)
        {
            if (jwtSettingDto == null)
            {
                throw new ArgumentNullException(nameof(jwtSettingDto));
            }

            var tokenHandler = new JwtSecurityTokenHandler();
            var key          = Encoding.ASCII.GetBytes(!isGoogleLogin
                ? user.Password
                : user.GooglePassword);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new[]
                {
                    new Claim(UserClaimTypes.UserId, user.Id),
                    new Claim(UserClaimTypes.Email, user.Email),
                    new Claim(UserClaimTypes.IsGoogleLogin, isGoogleLogin.ToString()),
                }),
                Expires            = DateTimeExtension.Get().AddDays(jwtSettingDto.ExpiredInDays),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }
Example #2
0
        private JwtSettingDto GetJwtSetting()
        {
            var jwtSetting = new JwtSettingDto();

            _configuration
            .GetSection(AppSettingKeys.JwtSettingSection)
            .Bind(jwtSetting);
            return(jwtSetting);
        }