【发布时间】:2017-03-17 10:48:44
【问题描述】:
我正在尝试根据this page 使用ProtectedConfigurationProvider 保护.Net 桌面应用程序的配置文件。我实现了一个新的提供程序类,以保护配置部分,我将部分节点反序列化为模型,加密内部字符串值,然后将加密模型序列化为部分节点并放入新的“EncryptedData”元素,反之亦然。 例如,我在配置文件中有一个“appSettings”部分:
<appSettings>
<add key="test key" value="test value" />
</appSettings>
加密后:
<appSettings configProtectionProvider="customProtectionProvider">
<EncryptedData>
<appSettings>
<add key="6ZefRBry+Q" value="6ZefRB2w7OuU" />
</appSettings>
</EncryptedData>
</appSettings>
这是我遇到的问题:当我尝试解密受保护的配置数据时,当我将加密部分 xml 反序列化为模型时,我的自定义提供程序中的 Decrypt 方法将始终被触发,然后再次进入反序列化部分。
-
加载配置并获取“appSettings”部分
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSection appSettingsSection = config.GetSection("appSettings"); // fire Decrypt method here // ...... -
在提供者的解密方法中
var sectionModel = ConfigurationBase.Deserialize(encryptedNode); -
反序列化
//...get custom section type by encryptedNode.Name Type sectionType = typeof(Section.AppSettingsSection); XmlSerializer serializer = new XmlSerializer(sectionType); // fire Decrypt method here and then infinite loops
这是我的 AppSettingsSection 类:
[XmlRoot("appSettings")]
public class AppSettingsSection : ConfigurationBase
{
[XmlAttribute("file")]
public string File { get; set; }
[XmlElement("add")]
public List<KeyValueNode> Settings { get; set; }
protected override void encryp()
{
// ......
}
protected override void decrypt()
{
// ......
}
}
我不知道为什么创建这种类型的 XmlSerializer 会调用 ProtectedConfigurationProvider 的 Decrypt 方法。
有什么解决办法吗?
【问题讨论】:
-
太奇怪了...当我在 vs2010 中构建项目时,而不是像以前那样在 vs2012 中构建项目,一切正常。