From 154478c31838215d32b062cdd5351382aa41d887 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 16 Dec 2024 17:20:40 +0100 Subject: [PATCH] =?UTF-8?q?feat(ParamsConfigureOptions):=20Erstellt,=20um?= =?UTF-8?q?=20nach=20der=20Konfiguration=20=C3=BCber=20appsettings=20initi?= =?UTF-8?q?alisiert=20zu=20werden.=20=20-=20DI=20Extension=20Methoden=20wu?= =?UTF-8?q?rden=20entsprechend=20bearbeitet.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Config/ParamsConfigureOptions.cs | 9 +++++ DigitalData.Core.Security/DIExtensions.cs | 35 +++++++++++-------- 2 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 DigitalData.Core.Security/Config/ParamsConfigureOptions.cs diff --git a/DigitalData.Core.Security/Config/ParamsConfigureOptions.cs b/DigitalData.Core.Security/Config/ParamsConfigureOptions.cs new file mode 100644 index 0000000..90be984 --- /dev/null +++ b/DigitalData.Core.Security/Config/ParamsConfigureOptions.cs @@ -0,0 +1,9 @@ +using Microsoft.Extensions.Options; + +namespace DigitalData.Core.Security.Config +{ + public class ParamsConfigureOptions : IConfigureOptions where TParams : RSAFactoryParams + { + public void Configure(TParams options) => options.Init(); + } +} \ No newline at end of file diff --git a/DigitalData.Core.Security/DIExtensions.cs b/DigitalData.Core.Security/DIExtensions.cs index 208ff04..e70c86a 100644 --- a/DigitalData.Core.Security/DIExtensions.cs +++ b/DigitalData.Core.Security/DIExtensions.cs @@ -20,11 +20,14 @@ namespace DigitalData.Core.Security options.Converters.Add(new JsonStringEnumConverter()); return options; } - + + private static IServiceCollection AddParamsConfigureOptions(this IServiceCollection services) where TParams : RSAFactoryParams + => services.AddSingleton, ParamsConfigureOptions>(); + private static IServiceCollection AddAsymCryptService(this IServiceCollection services, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams => setAsDefault - ? services.AddSingleton>() - : services.AddSingleton, AsymCryptService>(); + ? services.AddParamsConfigureOptions().AddSingleton>() + : services.AddParamsConfigureOptions().AddSingleton, AsymCryptService>(); /// /// Registers a custom asym crypt service with specified parameters from the given configuration section. @@ -34,9 +37,9 @@ namespace DigitalData.Core.Security /// /// If true, the factory is registered as the default . Otherwise, it is registered as . /// - public static IServiceCollection AddAsymCryptService(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false) - where TAsymCryptParams : AsymCryptParams - => services.Configure(section).AddAsymCryptService(setAsDefault: setAsDefault); + public static IServiceCollection AddAsymCryptService(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams => services + .Configure(section) + .AddAsymCryptService(setAsDefault: setAsDefault); /// /// Registers a custom asym crypt service with default parameters from the given configuration section. @@ -56,9 +59,9 @@ namespace DigitalData.Core.Security /// /// If true, the factory is registered as the default . Otherwise, it is registered as . /// - public static IServiceCollection AddAsymCryptService(this IServiceCollection services, TAsymCryptParams param, bool setAsDefault = false) - where TAsymCryptParams : AsymCryptParams - => services.AddSingleton(Options.Create(param)).AddAsymCryptService(setAsDefault: setAsDefault); + public static IServiceCollection AddAsymCryptService(this IServiceCollection services, TAsymCryptParams param, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams => services + .AddSingleton(Options.Create(param)) + .AddAsymCryptService(setAsDefault: setAsDefault); /// /// Registers default asym crypt service with the specified parameters from the given instance. @@ -66,7 +69,8 @@ namespace DigitalData.Core.Security /// /// /// - public static IServiceCollection AddAsymCryptService(this IServiceCollection services, AsymCryptParams param) => services.AddAsymCryptService(param: param, setAsDefault: true); + public static IServiceCollection AddAsymCryptService(this IServiceCollection services, AsymCryptParams param) => services + .AddAsymCryptService(param: param, setAsDefault: true); /// /// Registers default RSA Factory instance with default params @@ -74,8 +78,9 @@ namespace DigitalData.Core.Security /// /// /// The updated with the RSA Factory registered. - public static IServiceCollection AddRSAFactory(this IServiceCollection services, RSAFactoryParams? factoryParams = null) - => services.AddScoped(_ => new RSAFactory(Options.Create(factoryParams ?? new()))); + public static IServiceCollection AddRSAFactory(this IServiceCollection services, RSAFactoryParams? factoryParams = null) => services + .AddParamsConfigureOptions() + .AddScoped(_ => new RSAFactory(Options.Create(factoryParams ?? new()))); /// /// Registers a custom RSA Factory with specified parameters from the given configuration section. @@ -88,7 +93,7 @@ namespace DigitalData.Core.Security public static IServiceCollection AddRSAFactory(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false) where TRSAFactoryParams : RSAFactoryParams { - services.Configure(section); + services.AddParamsConfigureOptions().Configure(section); return setAsDefault ? services.AddSingleton>() : services.AddSingleton, RSAFactory>(); @@ -107,8 +112,8 @@ namespace DigitalData.Core.Security { services.AddSingleton(Options.Create(rsaParams)); return setAsDefault - ? services.AddSingleton>() - : services.AddSingleton, RSAFactory>(); + ? services.AddParamsConfigureOptions().AddSingleton>() + : services.AddParamsConfigureOptions().AddSingleton, RSAFactory>(); } } } \ No newline at end of file