【问题标题】:Is it possible to encrypt a config file specified as a configSource from web.config?是否可以加密从 web.config 指定为 configSource 的配置文件?
【发布时间】:2017-03-31 18:04:12
【问题描述】:

我的 web.config 中有一个自定义部分需要加密。这个自定义配置部分使用configSource 属性指向一个单独的配置文件(因为这个文件不是源代码控制的),我希望这个单独的配置文件被加密。我没有任何运气使用aspnet_regiis.exe 加密此部分。

我正在努力实现的目标可能吗?

我的 web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="protectedAppSettings" type="System.Configuration.NameValueSectionHandler, System,Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />       
    </configSections>       
    <protectedAppSettings configSource="config\EnvironmentConfigurations\ProtectedAppSettings.config" />
  </configuration>

我的自定义配置文件:

<?xml version="1.0" encoding="utf-8"?>
<protectedAppSettings>  
  <add key="XXX" value="xxx"/>
</protectedAppSettings>

我已将 aspnet_regiis 添加到我的路径中,以便我可以从我网站的根目录调用它。这是我正在执行的命令:

aspnet_regiis -pef protectedAppSettings ""

我从此命令得到的输出告诉我加密成功

我发现 this link 说它应该正常工作,但它不适合我..

【问题讨论】:

    标签: asp.net iis encryption web-config aspnet-regiis.exe


    【解决方案1】:

    这是因为我用来定义配置部分的类型。尽管没有文档可以证明这一点,但似乎 NameValueSectionHandler 类型在用于配置源时不会加密。解决方案是将类型更改为System.Configuration.AppSettingsSection,加密工作正常

    【讨论】:

    • 谢谢。这解决了我的问题。以下是完整参考:
    • 另外,如果有人想知道,这些值仍然会像 NameValue 类型一样被检索: var section = ConfigurationManager.GetSection("protectedAppSettings") as NameValueCollection; if (section == null) throw new Exception("Cannot read section 'protectedAppSettings' from the configuration file."); string xxx = section["XXX"] as string;
    • 我还必须指定强类型程序集,否则 aspnet_regiis 无法找到 AppSettingsSection 类。与@AlekDavis 类似,但使用最新的.NET 版本,我的完整部分行是:&lt;section name="protectedAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /&gt;
    猜你喜欢
    • 1970-01-01
    • 2014-05-03
    • 2015-11-27
    • 1970-01-01
    • 2021-04-12
    • 2011-03-06
    • 2011-12-19
    • 2020-11-24
    • 2016-01-30
    相关资源
    最近更新 更多