optimise codes

master
Young 7 months ago
parent 86c88a7dca
commit a4557135b7

@ -1,13 +1,8 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Claims;
using Infrastructure.Options;
using Infrastructure.Security;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.IdentityModel.Tokens;
using NetTaste;
namespace Infrastructure.Extensions;
@ -33,13 +28,7 @@ public static class AuthenticationSetup
return services;
}
services.TryAddSingleton<JwtSecurityTokenHandler>();
services.TryAddScoped(typeof(IUserContext<>), typeof(UserContextBase<>));
services.TryAddSingleton<DefaultTokenHandler>();
services.TryAddSingleton<IEncryptionService, EncryptionService>();
services.TryAddSingleton<IPostConfigureOptions<JwtBearerOptions>, JwtBearerOptionsPostConfigureOptions>();
services.TryAddSingleton(typeof(ITokenBuilder), typeof(TokenBuilderBase<>));
var key = configuration["AUDIENCE_KEY"] ?? audienceOptions.Secret;
ArgumentException.ThrowIfNullOrEmpty(key);
var buffer = Encoding.UTF8.GetBytes(key);

@ -1,6 +1,4 @@
using Infrastructure.Options;
using Infrastructure.Security;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
namespace Infrastructure.Extensions;

@ -1,7 +1,6 @@
using Infrastructure.Filters;
using Infrastructure.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

@ -0,0 +1,11 @@
namespace Infrastructure.Extensions;
public static class EncryptionSetup
{
public static IServiceCollection AddAesEncryption(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);
services.TryAddSingleton<IEncryptionService, EncryptionService>();
return services;
}
}

@ -1,6 +1,4 @@
using Infrastructure.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using StackExchange.Redis;
namespace Infrastructure.Extensions;

@ -0,0 +1,15 @@
namespace Infrastructure.Extensions;
public static class TokenContextSetup
{
public static IServiceCollection AddDefaultTokenContext(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);
services.TryAddSingleton<JwtSecurityTokenHandler>();
services.TryAddSingleton<DefaultTokenHandler>();
services.TryAddSingleton<ITokenBuilder, DefaultTokenBuilder>();
var builder = new DefaultTokenBuilder(null, null, null);
builder.SetUserContext(null);
return services;
}
}

@ -0,0 +1,11 @@
namespace Infrastructure.Extensions;
public static class UserContextSetup
{
public static IServiceCollection AddDefaultUserContext(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);
services.TryAddScoped(typeof(IUserContext<>), typeof(DefaultUserContext));
return services;
}
}

@ -3,7 +3,6 @@ using Infrastructure.Repository;
using Infrastructure.Utils;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Mvc;

@ -1,5 +1,3 @@
using System.IdentityModel.Tokens.Jwt;
namespace Infrastructure.Security;
public class DefaultTokenBuilder(

@ -1,4 +1,3 @@
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
namespace Infrastructure.Security;

@ -0,0 +1,5 @@
namespace Infrastructure.Security;
public class DefaultUserContext : UserContextBase<long>
{
}

@ -1,5 +1,4 @@
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Infrastructure.Utils;
using Microsoft.AspNetCore.Authentication.JwtBearer;

@ -28,8 +28,4 @@ public abstract class UserContextBase<TId> : IUserContext<TId> where TId : IEqua
public string[] RoleIds { get; set; }
public string RemoteIpAddress { get; set; }
}
public class DefaultUserContext : UserContextBase<long>
{
}
Loading…
Cancel
Save