Support encrypted cache conn strings & update dependencies

Moved DecryptConnectionString to KBR.Shared.Extensions for reuse and improved configuration access. Added Encryption property to CacheOptions and appsettings for conditional decryption of cache connection strings. Updated ServiceCollectionExtensions to decrypt SQL Server cache connection strings when needed. Upgraded NuGet packages across projects to latest .NET 8/10.0.x versions. Included minor code cleanups and OpenApiSecurityScheme improvements.
This commit is contained in:
Bangara Raju Kottedi 2026-02-15 14:22:22 +05:30
parent 4ebd93d39b
commit 2533b5298f
9 changed files with 40 additions and 23 deletions

View File

@ -10,10 +10,10 @@
<ItemGroup>
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.1" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="AutoMapper" Version="15.0.1" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.1" />
<PackageReference Include="AutoMapper" Version="16.0.0" />
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.9">

View File

@ -12,7 +12,7 @@ using PortBlog.API.Middleware;
using Serilog;
using System.Reflection;
using System.Text;
using KBR.Shared.Extensions;
var builder = WebApplication.CreateBuilder(args);
@ -131,14 +131,13 @@ builder.Services.AddSwaggerGen(c =>
Scheme = "ApiKeyScheme"
});
var key = new OpenApiSecurityScheme()
var key = new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "ApiKey"
},
In = ParameterLocation.Header
}
};
// JWT Bearer Security Definition

View File

@ -6,6 +6,7 @@
},
"Cache": {
"ConnectionString": "SERVER=192.168.0.197; DATABASE=cv_blog; UID=PortBlogDevUser; PWD=p@$$w0rd1234;Allow User Variables=true;",
"Encryption": "false",
"Provider": "SqlServer"
},
"Jwt": {

View File

@ -8,10 +8,16 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="coverlet.collector" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>

View File

@ -1,5 +1,6 @@
using KBR.Cache.Constants;
using KBR.Cache.Options;
using KBR.Shared.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
@ -20,9 +21,14 @@ namespace KBR.Cache.Extensions
}
else if (cacheOptions.Provider == CacheProviders.SqlServer)
{
var connectionString = cacheOptions.ConnectionString;
if ((bool)cacheOptions.Encryption)
{
connectionString = configuration.DecryptConnectionString(connectionString);
}
services.AddDistributedMySqlCache(options =>
{
options.ConnectionString = cacheOptions.ConnectionString;
options.ConnectionString = connectionString;
options.TableName = "Cache";
});
}

View File

@ -7,14 +7,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Options" Version="10.0.3" />
<PackageReference Include="Pomelo.Extensions.Caching.MySql" Version="2.2.1" />
</ItemGroup>

View File

@ -4,6 +4,8 @@
{
public string Provider { get; set; } = null!;
public bool? Encryption { get; set; } = false;
public string? ConnectionString { get; set; }
}
}

View File

@ -1,12 +1,14 @@
using System.Security.Cryptography;
using Microsoft.Extensions.Configuration;
using System.Security.Cryptography;
namespace PortBlog.API.Extensions
namespace KBR.Shared.Extensions
{
public static class ConfigurationExtensions
{
public static string DecryptConnectionString(this IConfiguration configuration, string encryptedConnectionString)
{
string keyBase64 = configuration.GetValue<string>("ConnectionStrings:Key").ToString();
// Fix: Use GetSection and Value instead of GetValue (since GetValue is not available on IConfiguration)
string keyBase64 = configuration.GetSection("ConnectionStrings:Key").Value;
string vectorBase64 = encryptedConnectionString.Split(":")[1];
string cipherText = encryptedConnectionString.Split(":")[0];

View File

@ -7,7 +7,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.3" />
</ItemGroup>
<ItemGroup>