From 0c2f6176b15df4b75f2451a497c583fe8b8af1fc Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 20 Jan 2025 15:39:33 +0800 Subject: [PATCH] Added `Encrypting Setting Values in the Application Configuration`section. Resolve #21900 --- docs/en/framework/infrastructure/settings.md | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/docs/en/framework/infrastructure/settings.md b/docs/en/framework/infrastructure/settings.md index 519d2795c3..ef164e939b 100644 --- a/docs/en/framework/infrastructure/settings.md +++ b/docs/en/framework/infrastructure/settings.md @@ -160,6 +160,39 @@ Setting values should be configured under the `Settings` section as like in this > `IConfiguration` is an .NET Core service and it can read values not only from the `appsettings.json`, but also from the environment, user secrets... etc. See [Microsoft's documentation](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/) for more. +#### Encrypting Setting Values in the Application Configuration + +You have to use the `ISettingEncryptionService` to encrypt the setting values when the `IsEncrypted` property of a setting definition is set to `true`, which means that the setting value should be encrypted in the `appsettings.json`. + +Example of injecting the `ISettingEncryptionService` into a class and encrypting the plain text: + +````csharp +public class MyService +{ + private readonly ISettingEncryptionService _encryptionService; + + public MyService(ISettingEncryptionService encryptionService) + { + _encryptionService = encryptionService; + } + + public void EncryptValue(string plainText) + { + var ciphertext = _encryptionService.Encrypt(plainText); + } +} +```` + +Then, Replace `ENCRYPTED_VALUE_HERE` with the ciphertext generated by `ISettingEncryptionService.Encrypt` method. + +````json +{ + "Settings": { + "MySetting": "ENCRYPTED_VALUE_HERE" + } +} +```` + ### Custom Setting Value Providers If you need to extend the setting system, you can define a class derived from the `SettingValueProvider` class. Example: