【问题标题】:ProtectedConfigurationProvider.Decrypt keep fired when decrypt config section nodeProtectedConfigurationProvider.Decrypt 在解密配置节节点时保持触发
【发布时间】: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 方法将始终被触发,然后再次进入反序列化部分。

  1. 加载配置并获取“appSettings”部分

    Configuration config = 
        ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConfigurationSection appSettingsSection = 
        config.GetSection("appSettings"); // fire Decrypt method here
        // ......
  2. 在提供者的解密方法中

    var sectionModel = ConfigurationBase.Deserialize(encryptedNode);

  3. 反序列化

    
    //...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 中构建项目,一切正常。

标签: c# config


【解决方案1】:

我下载了 System.Xml.Serialization 的调试符号并进入它,然后我发现TempAssembly 类的 65 行之后,ProtectedConfigurationProvider.Decrypt 方法将被调用,所以我检查了TempAssembly.UseLegacySerializerGeneration 属性,在 getter 中调用System.Xml.Serialization.AppSettings 类,它试图从ConfigurationManager.AppSettings 配置部分获取值,但我之前已经对其进行了加密,所以再次调用Decrypt 方法。

最后的解决方案是避免加密appSettings cofnig 部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    相关资源
    最近更新 更多