feat: implement read and write methods for json file storage provider

This commit is contained in:
Stevan Freeborn
2026-03-29 20:46:45 -05:00
parent b41669cbc6
commit 38daead9b2
12 changed files with 266 additions and 6 deletions
@@ -0,0 +1,6 @@
namespace StevanFreeborn.Extensions.Configuration.Secure.Sample;
public sealed record ApiOptions
{
public string ApiKey { get; init; } = string.Empty;
}
@@ -0,0 +1,38 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using StevanFreeborn.Extensions.Configuration.Secure.Sample;
using StevanFreeborn.Extensions.Configuration.Secure.Storage;
var builder = Host.CreateApplicationBuilder();
const string configFileName = "appsettings.json";
var opts = new JsonStorageOptions()
{
FileName = configFileName,
};
var storageProvider = new JsonFileStorageProvider(opts);
builder.Configuration.AddSecureConfig(storageProvider);
builder.Services.Configure<ApiOptions>(
builder.Configuration.GetSection(nameof(ApiOptions))
);
builder.Services.AddSecureConfig()
.UseJsonFileStorage(opt => opt.FileName = configFileName);
var app = builder.Build();
var options = app.Services.GetRequiredService<IOptions<ApiOptions>>();
var secureConfig = app.Services.GetRequiredService<ISecureConfig>();
Console.WriteLine(options.Value);
await secureConfig.SetAsync(
nameof(ApiOptions),
new ApiOptions { ApiKey = "apiKey" }
);
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StevanFreeborn.Extensions.Configuration.Secure\StevanFreeborn.Extensions.Configuration.Secure.csproj" />
</ItemGroup>
</Project>