AppSettings.Encryption 1.0.0

dotnet add package AppSettings.Encryption --version 1.0.0                
NuGet\Install-Package AppSettings.Encryption -Version 1.0.0                
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="AppSettings.Encryption" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AppSettings.Encryption --version 1.0.0                
#r "nuget: AppSettings.Encryption, 1.0.0"                
#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 AppSettings.Encryption as a Cake Addin
#addin nuget:?package=AppSettings.Encryption&version=1.0.0

// Install AppSettings.Encryption as a Cake Tool
#tool nuget:?package=AppSettings.Encryption&version=1.0.0                

AppSettings.Encryption is a simple yet powerful library that protects the data inside your settings using the Data Protection API provided by .NET Core. The library uses DPAPI, that ensures that the data is decryptable only on the machine that encrypted it.

Usage

  1. Install the package via NuGet:
dotnet add package AppSettings.Encryption
  1. Add the following code to your Program.cs file:
builder.Configuration.Sources.Clear();
builder.Configuration.AddProtectedJsonFile();
  1. To encrypt a setting value use the encryption prefix in you settings file: Say you have the following settings file:
{
  "SettingToEncrypt": "This is a value that will be encrypted"
}

To encrypt the value of SettingToEncrypt you can use the default encryption prefix |_@@_|:

{
  "|_@@_|SettingToEncrypt": "This is a value that will be encrypted"
}
  1. After the first run of your application, the library will automatically encrypt the value of SettingToEncrypt and update the settings file:
{
  "|@__@|SettingToEncrypt": "AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAA3J"
}
  1. Now you can access the decrypted value using the usual .NET Core configuration API:
var settingValue = _configuration.GetValue<string>("SettingToEncrypt");

Examples

You can find a simple example opening the AppSettingsEncryption.AspNetCoreExample project. More examples will be added in the future.

Other configurations

Custom settings file path

You can specify the path to the settings file by passing it as a parameter to the AddProtectedJsonFile method:

builder.Configuration.AddProtectedJsonFile("customnameappsettings.json");

You can also specify the entire file provider:

builder.Configuration.AddProtectedJsonFile(new PhysicalFileProvider(Directory.GetCurrentDirectory()), "customnameappsettings.json", false);

Custom encryption prefix

The encryption prefix (|_@@_| by default) and encrypted value prefix (|@__@| by default) can be customized choosing one of the overloads of the AddProtectedJsonFile method:

builder.Configuration.AddProtectedJsonFile("customEncryptionPrefix", "customEncryptedValuePrefix");

Remarks

  • Up to now the library is designed to work with JSON settings files only.
  • The library requires the access to the settings file to be able to encrypt the values in the first run. If the file is read-only or if the program has no write access to it, the library will throw an exception. This can be solved by executing the app with admin rights on the first run.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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.0 111 9/4/2024