是否可以保护appSettings部分中的单个元素而不是整个部分?

Ros*_*ick 5 .net c# app-config

我想保护我的appSettings中的一个键/值对,但不使用像我之前使用ProtectSection方法那样的其他东西,如下所示.

var configurationSection = config.GetSection("appSettings");
configurationSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想做类似以下的事情:

var configurationElement = config.GetSection("appSettings").GetElement("Protected");
configurationElement.ElementInformation.ProtectElement("DataProtectionConfigurationProvider");
Run Code Online (Sandbox Code Playgroud)

以下是我将要操作的示例appSettings:

<configuration>
<appSettings>
    <add key="Unprotected" value="ChangeMeFreely" />
    <add key="Protected" value="########"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我一直在寻找,但还没有办法做到这一点.这可能吗?

mar*_*c_s 5

不是开箱即用 - .NET为您提供了加密部分的能力 - 但不是单个元素.但是,由于这些只是字符串,因此您可以自己创建一些方案来加密字符串,然后将其保存到文件中,并在从配置文件中读取后对其进行解密.

但这不是透明的 - 你必须自己做,你必须明确地做.