A simple library that encrypt specific settings in your appsettings.json file
$ dotnet add package AppSettings.EncryptionAppSettings.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.
dotnet add package AppSettings.Encryption
Program.cs file:builder.Configuration.Sources.Clear();
builder.Configuration.AddProtectedJsonFile();
{
"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"
}
SettingToEncrypt and update the settings file:{
"|@__@|SettingToEncrypt": "AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAA3J"
}
var settingValue = _configuration.GetValue<string>("SettingToEncrypt");
You can find a simple example opening the AppSettingsEncryption.AspNetCoreExample project.
More examples will be added in the future.
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);
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");