optimise usercontext

master
Young 7 months ago
parent e5b016460e
commit 347ecdfdc3

@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using NetTaste;
namespace Infrastructure.Extensions; namespace Infrastructure.Extensions;
@ -33,11 +34,11 @@ public static class AuthenticationSetup
} }
services.TryAddSingleton<JwtSecurityTokenHandler>(); services.TryAddSingleton<JwtSecurityTokenHandler>();
services.TryAddScoped<IUserContext, UserContext>(); services.TryAddScoped(typeof(IUserContext<>), typeof(UserContextBase<>));
services.TryAddSingleton<DefaultTokenHandler>(); services.TryAddSingleton<DefaultTokenHandler>();
services.TryAddSingleton<IEncryptionService, EncryptionService>(); services.TryAddSingleton<IEncryptionService, EncryptionService>();
services.TryAddSingleton<IPostConfigureOptions<JwtBearerOptions>, JwtBearerOptionsPostConfigureOptions>(); services.TryAddSingleton<IPostConfigureOptions<JwtBearerOptions>, JwtBearerOptionsPostConfigureOptions>();
services.TryAddSingleton<ITokenBuilder, TokenBuilder>(); services.TryAddSingleton(typeof(ITokenBuilder<>),typeof(TokenBuilderBase<>));
var key = configuration["AUDIENCE_KEY"] ?? audienceOptions.Secret; var key = configuration["AUDIENCE_KEY"] ?? audienceOptions.Secret;
ArgumentException.ThrowIfNullOrEmpty(key); ArgumentException.ThrowIfNullOrEmpty(key);

@ -3,32 +3,31 @@ using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims; using System.Security.Claims;
using Infrastructure.Utils; using Infrastructure.Utils;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
namespace Infrastructure.Security; namespace Infrastructure.Security;
public interface ITokenBuilder public interface ITokenBuilder<TId> where TId : IEquatable<TId>
{ {
IList<Claim> GetClaimsFromUserContext(IUserContext userContext); IList<Claim> GetClaimsFromUserContext(IUserContext<TId> userContext);
void SetUserContext(TokenValidatedContext context); void SetUserContext(TokenValidatedContext context);
JwtTokenInfo GenerateJwtTokenInfo(IReadOnlyCollection<Claim> claims); JwtTokenInfo GenerateJwtTokenInfo(IReadOnlyCollection<Claim> claims);
} }
public class TokenBuilder( public abstract class TokenBuilderBase<TId>(
JwtOptions jwtOptions, JwtOptions jwtOptions,
JwtSecurityTokenHandler jwtSecurityTokenHandler, JwtSecurityTokenHandler jwtSecurityTokenHandler,
IEncryptionService encryptionService) IEncryptionService encryptionService)
: ITokenBuilder : ITokenBuilder<TId> where TId : IEquatable<TId>
{ {
public IList<Claim> GetClaimsFromUserContext(IUserContext userContext) public IList<Claim> GetClaimsFromUserContext(IUserContext<TId> userContext)
{ {
var claims = new List<Claim>() var claims = new List<Claim>()
{ {
new(JwtRegisteredClaimNames.UniqueName, userContext.Username), new(JwtRegisteredClaimNames.UniqueName, userContext.Username),
new(JwtRegisteredClaimNames.NameId, userContext.Id.ToString()), new(JwtRegisteredClaimNames.NameId, userContext.Id.ToString() ?? string.Empty),
new(JwtRegisteredClaimNames.Name, userContext.Name), new(JwtRegisteredClaimNames.Name, userContext.Name),
new(JwtRegisteredClaimNames.Email, userContext.Email), new(JwtRegisteredClaimNames.Email, userContext.Email),
new(JwtRegisteredClaimNames.Iat, new(JwtRegisteredClaimNames.Iat,
@ -42,11 +41,12 @@ public class TokenBuilder(
public void SetUserContext(TokenValidatedContext context) public void SetUserContext(TokenValidatedContext context)
{ {
var userContext = context.HttpContext.RequestServices.GetService<IUserContext>() ?? var userContext =
throw new NullReferenceException(nameof(IUserContext)); context.HttpContext.RequestServices.GetService(typeof(IUserContext<TId>)) as IUserContext<TId> ??
throw new NullReferenceException(nameof(IUserContext<TId>));
var principal = context.Principal ?? throw new NullReferenceException(nameof(context.Principal)); var principal = context.Principal ?? throw new NullReferenceException(nameof(context.Principal));
userContext.Id = long.Parse( var idClaim = principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.NameId);
principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.NameId).Value); userContext.Id = (TId)Convert.ChangeType(idClaim.Value, typeof(TId));
userContext.Username = userContext.Username =
principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.UniqueName).Value; principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.UniqueName).Value;
userContext.Name = principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Name).Value; userContext.Name = principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Name).Value;

@ -1,8 +1,8 @@
namespace Infrastructure.Security; namespace Infrastructure.Security;
public interface IUserContext public interface IUserContext<TId> where TId : IEquatable<TId>
{ {
long Id { get; set; } TId Id { get; set; }
string Username { get; set; } string Username { get; set; }
@ -15,9 +15,9 @@ public interface IUserContext
string RemoteIpAddress { get; set; } string RemoteIpAddress { get; set; }
} }
public class UserContext : IUserContext public abstract class UserContextBase<TId> : IUserContext<TId> where TId : IEquatable<TId>
{ {
public long Id { get; set; } public TId Id { get; set; }
public string Username { get; set; } public string Username { get; set; }

@ -4,7 +4,7 @@ namespace Infrastructure.Security;
public class JwtBearerOptionsPostConfigureOptions( public class JwtBearerOptionsPostConfigureOptions(
DefaultTokenHandler tokenHandler, DefaultTokenHandler tokenHandler,
ITokenBuilder tokenBuilder) ITokenBuilder<long> tokenBuilder)
: IPostConfigureOptions<JwtBearerOptions> : IPostConfigureOptions<JwtBearerOptions>
{ {
public void PostConfigure(string? name, JwtBearerOptions options) public void PostConfigure(string? name, JwtBearerOptions options)

Loading…
Cancel
Save