From 9099610185bd23b42bb90cdb73556f4260be8b1d Mon Sep 17 00:00:00 2001 From: Khwezi Mngoma Date: Sat, 13 Jun 2026 23:41:02 +0200 Subject: [PATCH] Refactored AddLiteCharmsWebSecurity to be OS aware when it handles data protection keys --- LiteCharms.Features/Extensions/Api.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/LiteCharms.Features/Extensions/Api.cs b/LiteCharms.Features/Extensions/Api.cs index 96fb0ba..28d2875 100644 --- a/LiteCharms.Features/Extensions/Api.cs +++ b/LiteCharms.Features/Extensions/Api.cs @@ -2,6 +2,8 @@ using LiteCharms.Features.Api; using LiteCharms.Features.Api.Configuration; using LiteCharms.Features.Api.Sdk; +using Microsoft.AspNetCore.Hosting; +using System.Runtime.InteropServices; namespace LiteCharms.Features.Extensions; @@ -51,13 +53,20 @@ public static class Api return services; } - public static IServiceCollection AddLiteCharmsWebSecurity(this IServiceCollection services, IConfiguration configuration) + public static IServiceCollection AddLiteCharmsWebSecurity(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment environment) { - var keysFolder = Path.Combine("/app/shared-keys"); + string keysFolderPath; + + if (OperatingSystem.IsLinux()) + keysFolderPath = "/app/shared-keys"; + else + keysFolderPath = Path.Combine(environment.ContentRootPath, "obj", "DeveloperDataProtectionKeys"); + + if (!Directory.Exists(keysFolderPath)) Directory.CreateDirectory(keysFolderPath); services.AddDataProtection() - .PersistKeysToFileSystem(new DirectoryInfo(keysFolder)) - .SetApplicationName("MidrandBooks"); + .PersistKeysToFileSystem(new DirectoryInfo(keysFolderPath)) + .SetApplicationName("MidrandBookshop"); var configSection = configuration.GetSection(nameof(LiteCharmsSettings));