feat(api): persist data protection keys for Windows Service
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net8.0</TargetFrameworks>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
@@ -18,6 +18,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Extensions" Version="8.0.11" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.1" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="10.0.1" />
|
||||
|
||||
@@ -3,6 +3,7 @@ using DigitalData.MessagingService.Application.DependencyInjection;
|
||||
using DigitalData.MessagingService.Infrastructure.DependencyInjection;
|
||||
using DigitalData.MessagingService.Infrastructure.Services.Background;
|
||||
using HealthChecks.UI.Client;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using Serilog;
|
||||
@@ -58,6 +59,16 @@ try
|
||||
builder.WebHost.UseUrls(urls);
|
||||
}
|
||||
|
||||
// Data Protection — persist keys to file system so Windows Service (LOCAL SYSTEM) can read them
|
||||
// across restarts. Controlled via appsettings "DataProtection:PersistKeysToFileSystem".
|
||||
if (builder.Configuration.GetValue<bool>("DataProtection:PersistKeysToFileSystem"))
|
||||
{
|
||||
var keysDir = Path.Combine(AppContext.BaseDirectory, "dp-keys");
|
||||
builder.Services.AddDataProtection()
|
||||
.PersistKeysToFileSystem(new DirectoryInfo(keysDir))
|
||||
.ProtectKeysWithDpapi(protectToLocalMachine: true);
|
||||
}
|
||||
|
||||
// Use Serilog for logging
|
||||
builder.Host.UseSerilog((context, services, configuration) => configuration
|
||||
.ReadFrom.Configuration(context.Configuration)
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
"UseWindowsService": true,
|
||||
"Urls": "http://0.0.0.0:1415"
|
||||
},
|
||||
"DataProtection": {
|
||||
"PersistKeysToFileSystem": true
|
||||
},
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
|
||||
Reference in New Issue
Block a user