AppSettings.Encryption
1.0.0
dotnet add package AppSettings.Encryption --version 1.0.0
NuGet\Install-Package AppSettings.Encryption -Version 1.0.0
<PackageReference Include="AppSettings.Encryption" Version="1.0.0" />
paket add AppSettings.Encryption --version 1.0.0
#r "nuget: AppSettings.Encryption, 1.0.0"
// 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
- Install the package via NuGet:
dotnet add package AppSettings.Encryption
- Add the following code to your
Program.cs
file:
builder.Configuration.Sources.Clear();
builder.Configuration.AddProtectedJsonFile();
- 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"
}
- 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"
}
- 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 | Versions 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. |
-
net8.0
- Microsoft.Extensions.Configuration.Json (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Security.Cryptography.ProtectedData (>= 8.0.0)
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 |