diff --git a/src/Infrastructure/HttpUserContext/IUserContext.cs b/src/Infrastructure/HttpUserContext/IUserContext.cs index 784f907..2654923 100644 --- a/src/Infrastructure/HttpUserContext/IUserContext.cs +++ b/src/Infrastructure/HttpUserContext/IUserContext.cs @@ -57,7 +57,7 @@ public interface IUserContext where TId : IEquatable /// /// JwtTokenInfo GenerateTokenInfo(IList? claims = null, - double duration = 0, + int duration = 0, string schemeName = JwtBearerDefaults.AuthenticationScheme); /// diff --git a/src/Infrastructure/HttpUserContext/UserContext.cs b/src/Infrastructure/HttpUserContext/UserContext.cs index b9ecb70..3005cb2 100644 --- a/src/Infrastructure/HttpUserContext/UserContext.cs +++ b/src/Infrastructure/HttpUserContext/UserContext.cs @@ -59,27 +59,25 @@ public class UserContext( public string Email { - get => _email ??= principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Email).Value; + get => _email ??= GetClaimValue(JwtRegisteredClaimNames.Email); set => _email = value; } public string[] RoleIds { - get => _roleIds ??= principal.Claims.Where(c => c.Type == ClaimConstants.RoleId).Select(c => c.Value).ToArray(); + get => _roleIds ??= GetClaimValues(ClaimConstants.RoleId); set => _roleIds = value; } public string[] RoleNames { - get => _roleNames ??= principal.Claims.Where(c => c.Type == ClaimTypes.Role) - .Select(c => c.Value).ToArray(); + get => _roleNames ??= GetClaimValues(ClaimTypes.Role); set => _roleNames = value; } public string[] Permissions { - get => _permissions ??= principal.Claims.Where(c => c.Type == ClaimConstants.PermissionCode) - .Select(c => c.Value).ToArray(); + get => _permissions ??= GetClaimValues(ClaimConstants.PermissionCode); set => _permissions = value; } @@ -91,7 +89,7 @@ public class UserContext( public JwtTokenInfo GenerateTokenInfo( IList? claims = null, - double duration = 0, + int duration = 0, string schemeName = JwtBearerDefaults.AuthenticationScheme) { claims ??= GetClaimsFromUserContext(); @@ -104,9 +102,9 @@ public class UserContext( { Issuer = jwtContext.Issuer, Audience = jwtContext.Audience, - Claims = claims?.ToDictionary(c => c.Type, c => (object)c.Value), - NotBefore = DateTime.Now, - Expires = DateTime.Now.AddSeconds(duration), + Subject = new ClaimsIdentity(claims), + NotBefore = DateTime.UtcNow, + Expires = DateTime.UtcNow.AddSeconds(duration), SigningCredentials = jwtContext.SigningCredentials, }; var token = jsonWebTokenHandler.CreateToken(tokenDescriptor); @@ -123,7 +121,7 @@ public class UserContext( new(JwtRegisteredClaimNames.Name, Name), new(JwtRegisteredClaimNames.Email, Email), new(JwtRegisteredClaimNames.Iat, - EpochTime.GetIntDate(DateTime.Now).ToString(CultureInfo.InvariantCulture), + EpochTime.GetIntDate(DateTime.UtcNow).ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer64), new(JwtRegisteredClaimNames.Exp, TimeSpan.FromSeconds(jwtContext.Duration).ToString())