2026-04-02 19:56:35 -05:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2026-03-29 20:46:45 -05:00
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
2026-04-02 19:56:35 -05:00
|
|
|
using StevanFreeborn.Extensions.Configuration.Secure;
|
|
|
|
|
using StevanFreeborn.Extensions.Configuration.Secure.Configuration;
|
2026-03-29 20:46:45 -05:00
|
|
|
using StevanFreeborn.Extensions.Configuration.Secure.Sample;
|
|
|
|
|
using StevanFreeborn.Extensions.Configuration.Secure.Storage;
|
|
|
|
|
|
2026-04-02 19:56:35 -05:00
|
|
|
Action<ISecureConfigBuilder> configure = builder =>
|
2026-03-29 20:46:45 -05:00
|
|
|
{
|
2026-04-02 19:56:35 -05:00
|
|
|
builder
|
|
|
|
|
.WithMachineIdKey()
|
|
|
|
|
.WithAesCryptoProvider()
|
|
|
|
|
.UseJsonFileStorage(new JsonStorageOptions())
|
|
|
|
|
.AddJsonAotContext(AppJsonContext.Default);
|
2026-03-29 20:46:45 -05:00
|
|
|
};
|
|
|
|
|
|
2026-04-02 19:56:35 -05:00
|
|
|
var builder = Host.CreateDefaultBuilder()
|
|
|
|
|
.ConfigureAppConfiguration((_, b) =>
|
|
|
|
|
{
|
|
|
|
|
b.AddSecureConfig(configure);
|
|
|
|
|
})
|
|
|
|
|
.ConfigureServices((ctx, s) =>
|
|
|
|
|
{
|
|
|
|
|
s.Configure<ApiOptions>(ctx.Configuration.GetSection(nameof(ApiOptions)));
|
|
|
|
|
s.AddSecureConfig(configure);
|
|
|
|
|
});
|
2026-03-29 20:46:45 -05:00
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
var options = app.Services.GetRequiredService<IOptions<ApiOptions>>();
|
2026-04-02 19:56:35 -05:00
|
|
|
var optionsMonitor = app.Services.GetRequiredService<IOptionsMonitor<ApiOptions>>();
|
2026-03-29 20:46:45 -05:00
|
|
|
var secureConfig = app.Services.GetRequiredService<ISecureConfig>();
|
2026-04-02 19:56:35 -05:00
|
|
|
var scopeFactory = app.Services.GetRequiredService<IServiceScopeFactory>();
|
2026-03-29 20:46:45 -05:00
|
|
|
|
2026-04-02 19:56:35 -05:00
|
|
|
var firstScope = scopeFactory.CreateScope();
|
|
|
|
|
var firstSnapshot = firstScope.ServiceProvider.GetRequiredService<IOptionsSnapshot<ApiOptions>>();
|
|
|
|
|
|
|
|
|
|
var originalValue = await secureConfig.GetAsync<ApiOptions>(nameof(ApiOptions));
|
|
|
|
|
Console.WriteLine($"Config: {originalValue}");
|
|
|
|
|
Console.WriteLine($"IOptions: {options.Value}");
|
|
|
|
|
Console.WriteLine($"IOptionsSnapshot 1: {firstSnapshot.Value}");
|
|
|
|
|
Console.WriteLine($"IOptionsMonitor: {optionsMonitor.CurrentValue}");
|
2026-03-29 20:46:45 -05:00
|
|
|
|
|
|
|
|
await secureConfig.SetAsync(
|
|
|
|
|
nameof(ApiOptions),
|
2026-04-02 19:56:35 -05:00
|
|
|
new ApiOptions { ApiKey = Guid.NewGuid().ToString() }
|
2026-03-29 20:46:45 -05:00
|
|
|
);
|
2026-04-02 19:56:35 -05:00
|
|
|
|
|
|
|
|
var config = (IConfigurationRoot)app.Services.GetRequiredService<IConfiguration>();
|
|
|
|
|
config.Reload();
|
|
|
|
|
|
|
|
|
|
var updatedValue = await secureConfig.GetAsync<ApiOptions>(nameof(ApiOptions));
|
|
|
|
|
var secondScope = scopeFactory.CreateScope();
|
|
|
|
|
var secondSnapshot = secondScope.ServiceProvider.GetRequiredService<IOptionsSnapshot<ApiOptions>>();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"Config: {updatedValue}");
|
|
|
|
|
Console.WriteLine($"IOptions: {options.Value}");
|
|
|
|
|
Console.WriteLine($"IOptionsSnapshot 2: {secondSnapshot.Value}");
|
|
|
|
|
Console.WriteLine($"IOptionsMonitor: {optionsMonitor.CurrentValue}");
|