AppsettingsProtector 0.9.5
dotnet add package AppsettingsProtector --version 0.9.5
NuGet\Install-Package AppsettingsProtector -Version 0.9.5
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="AppsettingsProtector" Version="0.9.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AppsettingsProtector --version 0.9.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AppsettingsProtector, 0.9.5"
#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 AppsettingsProtector as a Cake Addin #addin nuget:?package=AppsettingsProtector&version=0.9.5 // Install AppsettingsProtector as a Cake Tool #tool nuget:?package=AppsettingsProtector&version=0.9.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Appsettings Protector
About
This library can protect your JSON app settings using the Microsoft.AspNetCore.DataProtection library.
Quickstart
- Install the nuget package.
- Add a separate
protected.json
file to store your secrets or confidential information (you can name the file anything really, you don't have to even use the .json extension) - but make sure to reference this file name in your startup code.- Make sure it's properly formed JSON, the library will use
System.Text.Json.JsonNode.Parse()
to do some basic validation.
- Make sure it's properly formed JSON, the library will use
- Call two extension methods on your program's startup code in this order:
- First on the host service collection:
AddPersistedEncryptor()
- This will register the encryptor used to encrypt the file the first time which you will then pass to the next method.
- Second, the host configuration builder:
AddEncryptedJsonFile()
- This will register a new config source that will transparently decrypt the encrypted JSON file on program startup.
- First on the host service collection:
public static void Main(string[] args)
{
// this example uses a Blazor web app (.NET 6 - so no Startup class)
var builder = WebApplication.CreateBuilder(args);
// first method
builder.Services.AddPersistedEncryptorWithDefaults(out var startupEncryptor);
// second method
builder.Configuration.AddEncryptedJsonFile(source => {
source.Path = "protectedSettings.json";
source.Encryptor = startupEncryptor;
source.TryEncryptOnDecryptFailure = true; // this is true anyway, but code is here to demonstrate the api exists
});
// should now be able to access encrypted JSON values!
var secret = builder.Configuration["secret"];
if (secret == null) {
throw new ArgumentNullException(nameof(secret));
}
}
FAQs
- How does it handle first-time encryption?
- This logic is: always attempt decryption of the specified JSON file every time on startup, but if it fails, try parsing it as JSON, and if no errors occur during this JSON-parsing phase, assume it's un-encrypted and then encrypt it.
- This should properly handle first time encryption, and the same logic repeats every subsequent time startup occurs.
- What are the defaults?
- Uses the ASP.NET Core Data Protection library, but:
- Disables automatic key generation, so the key is not rotated every 90 days, but this can be overriden by you, so the library will also...
- ...default to using the DangerouUnprotect() API.
- On Windows the key used by itself is protected using the Windows CN-DPAPI (so the key is accessed via currently logged in user's SID).
- Uses the ASP.NET Core Data Protection library, but:
- Can I encrypt the protected JSON file before deployment?
- Currently, no: as it is, the library is designed to encrypt the file for you, after deployment, and then transparently decrypt the file each time the app starts up.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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.
-
.NETStandard 2.0
- Microsoft.AspNetCore.DataProtection.Extensions (>= 7.0.10)
- Microsoft.Extensions.Configuration.Json (>= 7.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 7.0.0)
- OneOf (>= 3.0.263)
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 |
---|---|---|
0.9.5 | 264 | 9/15/2023 |