public string CreateToken(IMiCakeUser miCakeUser) { IDictionary <string, object> claimCollection = null; var userProperties = miCakeUser.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (var userProperty in userProperties) { var jwtProperty = userProperty.GetCustomAttribute <JwtClaimAttribute>(); if (jwtProperty != null) { claimCollection = claimCollection ?? new Dictionary <string, object>(); claimCollection.Add(jwtProperty.ClaimName == null ? userProperty.Name.ToLower() : jwtProperty.ClaimName.ToLower(), userProperty.GetValue(miCakeUser).ToString()); } } var expireTime = DateTime.Now.AddMinutes(miCakeJwtOptions.ExpirationMinutes); var signingCredentials = new SigningCredentials(new SymmetricSecurityKey(miCakeJwtOptions.SecurityKey), miCakeJwtOptions.Algorithm); return(CreateTokenCore(miCakeJwtOptions.Issuer, miCakeJwtOptions.Audience, null, null, expireTime, null, signingCredentials, miCakeJwtOptions.EncryptingCredentials, claimCollection)); }
protected virtual SecurityTokenDescriptor CreateSecurityTokenDescriptorByMiCakeUser(IMiCakeUser miCakeUser, CancellationToken cancellationToken = default) { IDictionary <string, object> claimCollection = null; var userProperties = miCakeUser.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (var userProperty in userProperties) { var jwtProperty = userProperty.GetCustomAttribute <JwtClaimAttribute>(); if (jwtProperty != null) { claimCollection ??= new Dictionary <string, object>(); claimCollection.Add(jwtProperty.ClaimName == null ? userProperty.Name.ToLower() : jwtProperty.ClaimName.ToLower(), userProperty.GetValue(miCakeUser).ToString()); } } var expireTime = DateTime.Now.AddSeconds(_jwtOptions.AccessTokenLifetime); var signingCredentials = new SigningCredentials(new SymmetricSecurityKey(_jwtOptions.SecurityKey), _jwtOptions.Algorithm); return(new SecurityTokenDescriptor { Issuer = _jwtOptions.Issuer, Audience = _jwtOptions.Audience, Expires = expireTime, SigningCredentials = signingCredentials, EncryptingCredentials = _jwtOptions.EncryptingCredentials, Claims = claimCollection }); }