Fededim.Extensions.Configuration.ProtectedJson 1.0.2

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package Fededim.Extensions.Configuration.ProtectedJson --version 1.0.2
NuGet\Install-Package Fededim.Extensions.Configuration.ProtectedJson -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Fededim.Extensions.Configuration.ProtectedJson" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Fededim.Extensions.Configuration.ProtectedJson --version 1.0.2
#r "nuget: Fededim.Extensions.Configuration.ProtectedJson, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Fededim.Extensions.Configuration.ProtectedJson as a Cake Addin
#addin nuget:?package=Fededim.Extensions.Configuration.ProtectedJson&version=1.0.2

// Install Fededim.Extensions.Configuration.ProtectedJson as a Cake Tool
#tool nuget:?package=Fededim.Extensions.Configuration.ProtectedJson&version=1.0.2

About

Fededim.Extensions.Configuration.ProtectedJson is an improved JSON configuration provider which allows partial or full encryption of configuration values stored in appsettings.json files and fully integrated in the ASP.NET Core architecture. Basically, it implements a custom ConfigurationSource and a custom ConfigurationProvider defining a custom tokenization tag which whenever found decrypts the enclosed encrypted data using ASP.NET Core Data Protection API.

This package is however deprecated in favour of the more versatile Fededim.Extensions.Configuration.Protected.

Key Features

  • Encrypt partially or fully a configuration value
  • Trasparent in memory decryption of encrypted values without almost any additional line of code

How to Use

  • Modify appsettings JSON files by enclose with the encryption tokenization tag (e.g. Protect:{<data to be encrypted}) all the values or part of values you would like to encrypt
  • Configure the data protection api in a helper method (e.g. ConfigureDataProtection)
  • Encrypt all appsettings values by calling IDataProtect.ProtectFiles extension method (use ProtectedJsonConfigurationProvider.DataProtectionPurpose as CreateProtector purpose)
  • Define the application configuration using ConfigurationBuilder and adding encrypted json files using AddProtectedJsonFile extension method
  • Call ConfigurationBuilder.Build to automatically decrypt the encrypted values and retrieve the cleartext ones.
  • Map the Configuration object to a strongly typed hierarchical class using DI Configure

using Microsoft.Extensions.Configuration;
using Fededim.Extensions.Configuration.ProtectedJson;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
using Fededim.Extensions.Configuration.ProtectedJson.ConsoleTest;
using Microsoft.Extensions.Options;

public class Program
{
    private static void ConfigureDataProtection(IDataProtectionBuilder builder)
    {
        builder.UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration
        {
            EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
            ValidationAlgorithm = ValidationAlgorithm.HMACSHA256,

        }).SetDefaultKeyLifetime(TimeSpan.FromDays(365*15)).PersistKeysToFileSystem(new DirectoryInfo("..\\..\\..\\Keys"));
    }


    public static void Main(String[] args)
    {
        // define the DI services: Data Protection API
        var servicesDataProtection = new ServiceCollection();
        ConfigureDataProtection(servicesDataProtection.AddDataProtection());
        var serviceProviderDataProtection = servicesDataProtection.BuildServiceProvider();

        // retrieve IDataProtector interface for encrypting data
        var dataProtector = serviceProviderDataProtection.GetRequiredService<IDataProtectionProvider>().CreateProtector(ProtectedJsonConfigurationProvider.DataProtectionPurpose);

        // encrypt all Protect:{<data>} token tags of all .json files (must be done before reading the configuration)
        var encryptedFiles = dataProtector.ProtectFiles(".");

        // define the application configuration and read .json files
        var configuration = new ConfigurationBuilder()
                .AddCommandLine(args)
                .AddProtectedJsonFile("appsettings.json", ConfigureDataProtection)
                .AddProtectedJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNETCORE_ENVIRONMENT")}.json", ConfigureDataProtection)
                .AddEnvironmentVariables()
                .Build();

        // define other DI services: configure strongly typed AppSettings configuration class (must be done after having read the configuration)
        var services = new ServiceCollection();
        services.Configure<AppSettings>(configuration);
        var serviceProvider = services.BuildServiceProvider();

        // retrieve the strongly typed AppSettings configuration class
        var appSettings = serviceProvider.GetRequiredService<IOptions<AppSettings>>().Value;
    }
}

The main types provided by this library are:

  • Fededim.Extensions.Configuration.ProtectedJson.ProtectedJsonStreamConfigurationProvider
  • Fededim.Extensions.Configuration.ProtectedJson.ProtectedJsonStreamConfigurationSource
  • Fededim.Extensions.Configuration.ProtectedJson.ProtectedJsonConfigurationProvider
  • Fededim.Extensions.Configuration.ProtectedJson.ProtectedJsonConfigurationSource

Feedback & Contributing

Fededim.Extensions.Configuration.ProtectedJson is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 211 12/16/2023
1.0.1 121 12/4/2023
1.0.0 116 11/21/2023

v1.0.0
     - Initial commit

     v1.0.1
     - Targeting multi frameworks: net6.0, netstandard2.0 and net462

     v1.0.2
     - Renamed GitHub repository, just updated the NuGet README file. This package is however deprecated in favour of the more versatile [Fededim.Extensions.Configuration.Protected](Fededim.Extensions.Configuration.Protected).