【问题标题】:Encrypting custom sections of a web.config加密 web.config 的自定义部分
【发布时间】:2011-03-31 02:41:51
【问题描述】:

我使用文章Creating a Flexible Configuration Section Handler 在我的应用程序中创建了一个灵活的配置节处理程序。

我还看到了这篇题为 Encrypting Custom Configuration Sections on the OdeToCode blog 的文章,内容是关于如何加密 web.config 文件的某些部分。

从第一篇文章开始,我们就有了这个 web.config 代码。

<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionname="StyleSheetSettings_1"    
            type="FifteenSeconds.Core.BasicConfigurator"/>
    </configSections>
    <StyleSheetSettings_1>
        <StyleSheets>
            <Style SheetName="Page"Href="Styles/Page.css"Media="screen"/>
            <StyleSheetName="Custom"Href="Styles/Custom.css"Media="screen"/>
            <StyleSheetName="Print"Href="/Lib/Styles/Print.css"Media="print"/>
        </StyleSheets>      
    </StyleSheetSettings_1>
 </configuration>

我尝试使用以下代码来加密代码,使用类似于以下命令行代码的代码。

 aspnet_regiis.exe -pef  "StyleSheetSettings_1" C:\Test\

我收到以下错误

无法加载类型 15秒.Core.BasicConfigurator' 从程序集'System.Web, 版本=4.0.0.0,文化=中性, PublicKeyToken=b03f5f7f11d50a3a'。

任何帮助将不胜感激。

【问题讨论】:

    标签: asp.net configuration web-config


    【解决方案1】:

    这是解决此问题的另一种解决方法(位于 http://www.dotnetnoob.com/2013/01/how-to-encrypt-custom-configuration.html)。在运行 aspnet_regiis 命令之前,注释掉 configSections 元素 (/configuration/configSections) 下自定义部分的部分元素,并且自定义部分应该被加密。

    <configSections>
        <!--<section name="myCustomSection" type="My.Product.CustomSection, My.Product.Assembly/>-->
    </configSections>
    
    
    c:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis -pef myCustomSection C:\path\to\app
    Microsoft (R) ASP.NET RegIIS version 4.0.30319.17929
    Administration utility to install and uninstall ASP.NET on the local machine.
    Copyright (C) Microsoft Corporation.  All rights reserved.
    Encrypting configuration section...
    Succeeded!
    

    【讨论】:

    • 它成功了,谢谢您,先生!
    • 干得好,谢谢!有道理,只有应用程序需要将注释部分脱盐到指定类型,并且只会混淆加密。
    • 3 年过去了,这个 bug 仍然没有修复。天哪该死。
    • 7 年后,这是唯一有效的方法。我最后保存了这个解决方案,因为我认为它不可能是 7 年后的答案。完全限定的类型名称和完全限定的程序集名称对我不起作用。
    【解决方案2】:

    唯一已知的解决方案是可怕的 hack。将程序集(和所有依赖项)复制到相关的 .NET 框架目录(aspnet_regiis.exe 所在的位置)。

    【讨论】:

    【解决方案3】:

    尝试更改类型以包含程序集名称

    type="FifteenSeconds.Core.BasicConfigurator, MyWebApplication"
    

    这假设 BasicConfiguration 在您的 Web 应用中

    【讨论】:

    • 添加程序集名称 type="FifteenSeconds.Core.BasicConfigurator, FifteenSeconds.Core" 后,我仍然收到以下错误。正在加密配置部分...为 StyleSheetSettings_1 创建配置部分处理程序时出错:无法加载文件或程序集“FifteenSeconds.Core”或其依赖项之一。系统找不到指定的文件。
    • 相对于 Web.Config 的 DLL 在哪里
    【解决方案4】:

    在我的配置文件中引用类型时,我遇到了类似的问题。正如 Conrad Frix 建议的那样,您需要在命名空间类型引用之后引用程序集名称。我错误地记下了我认为的程序集名称,而不是检查它的名称可能与项目名称不同。您可以通过右键单击 Visual Studio 中的项目并转到属性来确保。仔细检查以确保项目输出的程序集与您在 web.config 中指定的名称相同。

    【讨论】:

      【解决方案5】:

      这样的东西可能有用,我自己没有尝试过,不是一个干净的解决方案

      http://blogs.msdn.com/b/kaevans/archive/2004/08/19/217177.aspx 它使用 System.Configuration.NameValueSectionHandler。

      (System.Collections.Specialized.NameValueCollection) WebConfigurationManager.GetSection("SectionName")
      

      我已经尝试过这种方式,使用 System.Configuration.SingleTagSectionHandler 和

      (Hashtable)WebConfigurationManager.GetSection("SectionName");
      

      http://vaultofthoughts.net/UsingSingleTagSectionHandlerInsteadOfAppSettings.aspx

      【讨论】:

        【解决方案6】:

        我刚刚很容易解决了一个类似的问题。您需要在“类型”属性中指定库。

        代替:

        <section name="StyleSheetSettings_1" type="FifteenSeconds.Core.BasicConfigurator"/>
        

        试试:

        <section name="StyleSheetSettings_1" type="FifteenSeconds.Core.BasicConfigurator, FifteenSeconds"/>
        

        我的问题几乎完全相同,尽管我使用的是 .NET 库。

        这个:

        <section name="Roles" type="System.Configuration.AppSettingsSection" />
        

        成为:

        <section name="Roles" type="System.Configuration.AppSettingsSection, System.Configuration" />
        

        希望这行得通。

        【讨论】:

          猜你喜欢
          • 2013-05-07
          • 1970-01-01
          • 2012-08-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多