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

标签 c# .net app-config

我想在我的 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/

相关文章:

c# - 返回 ICollection<T> 而不是 List<T> 的真正优势是什么?

.net - .NET 桌面应用程序中的 Settings.settings 与 app.config

c# - ConfigurationSettings.AppSettings 已过时,警告

c# - 正则表达式拆分未给出预期结果

c# - 使用循环在 HTML(MVC) 中显示列表字符串

c# - 空字符串作为特例?

visual-studio-2010 - nUnit 测试寻找 {assembly-name}.dll.config

c# - 为什么锁定我们要更改的对象是一种不好的做法?

c# - 在 EF 中添加触发器

c# - ContentControl.Template 和 ContentControl.ContentTemplate 有什么区别