我想在我的 appSettings 中保护一个键/值对,但不保护其他键/值对,就像我之前使用 ProtectSection 方法所做的那样,如下所示。
var configurationSection = config.GetSection("appSettings");
configurationSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
理想情况下,我想做如下事情:
var configurationElement = config.GetSection("appSettings").GetElement("Protected");
configurationElement.ElementInformation.ProtectElement("DataProtectionConfigurationProvider");
这是我要操作的示例 appSettings:
<configuration>
<appSettings>
<add key="Unprotected" value="ChangeMeFreely" />
<add key="Protected" value="########"/>
</appSettings>
</configuration>
我一直在寻找,但还没有找到执行此操作的方法。这可能吗?
最佳答案
并非开箱即用 - .NET 为您提供加密部分的能力 - 但不是单个元素。但是,由于这些只是字符串,您完全可以自己创建一些方案来在将字符串保存到文件之前对其进行加密,并在从配置文件中读取后对其进行解密。
但这不会是透明的 - 您必须自己做,并且必须明确地做。
关于c# - 是否可以保护 appSettings 部分中的单个元素而不是整个部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2732482/