Skip to content

Commit ae43cd1

Browse files
committed
feat: specify data protection key persistence location
1 parent 0fda04b commit ae43cd1

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

WebApi/Options/HostingOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ public class HostingOptions
99
/// Enable the HTTPS redirection middleware.
1010
/// </summary>
1111
public bool EnableHttpsRedirect { get; set; } = true;
12+
13+
/// <summary>
14+
/// The directory to store the data protection keys.
15+
/// </summary>
16+
public string? DataProtectionKeysPath { get; set; }
1217
}

WebApi/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
using BackgroundProcessor.Templates;
77
using Core.AppSettings;
88
using Microsoft.AspNetCore.Authorization;
9+
using Microsoft.AspNetCore.DataProtection;
10+
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
11+
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
912
using Microsoft.AspNetCore.Mvc;
1013
using Microsoft.Data.Sqlite;
1114
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
@@ -116,6 +119,19 @@ static void ConfigureServices(IServiceCollection services, IConfiguration config
116119
opts.FallbackPolicy = policy;
117120
});
118121

122+
var hostingOptions = configuration.GetSection("Hosting").Get<HostingOptions>();
123+
124+
if (!string.IsNullOrEmpty(hostingOptions?.DataProtectionKeysPath))
125+
{
126+
services.AddDataProtection()
127+
.PersistKeysToFileSystem(new DirectoryInfo(hostingOptions.DataProtectionKeysPath))
128+
.UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration
129+
{
130+
EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
131+
ValidationAlgorithm = ValidationAlgorithm.HMACSHA512,
132+
});
133+
}
134+
119135
services.Configure<RedditSettings>(configuration.GetSection("RedditSettings"));
120136

121137
services.AddMemoryCache();

0 commit comments

Comments
 (0)