diff --git a/src/Infrastructure/Extensions/TokenContextSetup.cs b/src/Infrastructure/Extensions/TokenContextSetup.cs index f908cf2..8ba205c 100644 --- a/src/Infrastructure/Extensions/TokenContextSetup.cs +++ b/src/Infrastructure/Extensions/TokenContextSetup.cs @@ -10,7 +10,7 @@ public static class TokenContextSetup public static IServiceCollection AddDefaultTokenContext(this IServiceCollection services) { ArgumentNullException.ThrowIfNull(services); - services.TryAddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(); return services; } diff --git a/src/Infrastructure/GlobalUsing.cs b/src/Infrastructure/GlobalUsing.cs index c116c37..93834d3 100644 --- a/src/Infrastructure/GlobalUsing.cs +++ b/src/Infrastructure/GlobalUsing.cs @@ -11,7 +11,8 @@ global using System.Reflection; global using System.Text; global using System.Text.Json; global using System.Threading; -global using System.IdentityModel.Tokens.Jwt; global using Infrastructure.Security; global using Microsoft.Extensions.DependencyInjection; -global using Microsoft.Extensions.DependencyInjection.Extensions; \ No newline at end of file +global using Microsoft.Extensions.DependencyInjection.Extensions; +global using Microsoft.IdentityModel.JsonWebTokens; +global using Microsoft.IdentityModel.Tokens; \ No newline at end of file diff --git a/src/Infrastructure/HttpUserContext/UserContext.cs b/src/Infrastructure/HttpUserContext/UserContext.cs index 2a8837c..38d06c7 100644 --- a/src/Infrastructure/HttpUserContext/UserContext.cs +++ b/src/Infrastructure/HttpUserContext/UserContext.cs @@ -3,7 +3,6 @@ using System.Security.Claims; using Infrastructure.Utils; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Http; -using Microsoft.IdentityModel.Tokens; namespace Infrastructure.HttpUserContext; @@ -13,13 +12,13 @@ namespace Infrastructure.HttpUserContext; /// /// /// -/// +/// /// public class UserContext( IHttpContextAccessor httpContextAccessor, JwtContext jwtContext, IEncryptionService encryptionService, - JwtSecurityTokenHandler jwtSecurityTokenHandler) + JsonWebTokenHandler jsonWebTokenHandler) : IUserContext where TId : IEquatable { private readonly ClaimsPrincipal principal = httpContextAccessor?.HttpContext?.User; @@ -48,13 +47,13 @@ public class UserContext( public string Username { - get => _username ??= principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.UniqueName).Value; + get => _username ??= GetClaimValue(JwtRegisteredClaimNames.UniqueName); set => _username = value; } public string Name { - get => _name ??= principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Name).Value; + get => _name ??= GetClaimValue(JwtRegisteredClaimNames.Name); set => _name = value; } @@ -101,14 +100,16 @@ public class UserContext( duration = jwtContext.Duration; } - var securityToken = new JwtSecurityToken( - issuer: jwtContext.Issuer, - audience: jwtContext.Audience, - claims: claims, - notBefore: DateTime.Now, - expires: DateTime.Now.AddSeconds(jwtContext.Duration), - signingCredentials: jwtContext.SigningCredentials); - var token = jwtSecurityTokenHandler.WriteToken(securityToken); + var tokenDescriptor = new SecurityTokenDescriptor() + { + Issuer = jwtContext.Issuer, + Audience = jwtContext.Audience, + Claims = claims?.ToDictionary(c => c.Type, c => (object)c.Value), + NotBefore = DateTime.Now, + Expires = DateTime.Now.AddSeconds(duration.Value), + SigningCredentials = jwtContext.SigningCredentials, + }; + var token = jsonWebTokenHandler.CreateToken(tokenDescriptor); token = encryptionService.Encrypt(token); return new JwtTokenInfo(token, duration.Value, schemeName); } @@ -142,4 +143,14 @@ public class UserContext( var idClaim = principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.NameId); return (TId)Convert.ChangeType(idClaim.Value, typeof(TId)); } + + private string GetClaimValue(string claimType) + { + return principal.Claims.First(c => c.Type == claimType).Value; + } + + private string[] GetClaimValues(string claimType) + { + return principal.Claims.Where(c => c.Type == claimType).Select(c => c.Value).ToArray(); + } } \ No newline at end of file diff --git a/src/Infrastructure/Security/DefaultTokenHandler.cs b/src/Infrastructure/Security/DefaultTokenHandler.cs index 03a595e..0bdac41 100644 --- a/src/Infrastructure/Security/DefaultTokenHandler.cs +++ b/src/Infrastructure/Security/DefaultTokenHandler.cs @@ -1,19 +1,17 @@ -using Microsoft.IdentityModel.Tokens; - namespace Infrastructure.Security; /// /// 自定义token解密 /// /// -/// -public class DefaultTokenHandler(IEncryptionService encryptionService, JwtSecurityTokenHandler jwtSecurityTokenHandler) +/// +public class DefaultTokenHandler(IEncryptionService encryptionService, JsonWebTokenHandler jwtTokenHandler) : TokenHandler { public override Task ValidateTokenAsync(string token, TokenValidationParameters validationParameters) { var decodeToken = encryptionService.Decrypt(token); - return jwtSecurityTokenHandler.ValidateTokenAsync(decodeToken, validationParameters); + return jwtTokenHandler.ValidateTokenAsync(decodeToken, validationParameters); } } \ No newline at end of file diff --git a/src/Infrastructure/Security/JwtContext.cs b/src/Infrastructure/Security/JwtContext.cs index 82e760f..ac8ebc4 100644 --- a/src/Infrastructure/Security/JwtContext.cs +++ b/src/Infrastructure/Security/JwtContext.cs @@ -1,5 +1,3 @@ -using Microsoft.IdentityModel.Tokens; - namespace Infrastructure.Security; ///