代码之家  ›  专栏  ›  技术社区  ›  John Hoven

使用ASPNet_Regis加密自定义配置部分-你能做到吗?

  •  37
  • John Hoven  · 技术社区  · 17 年前

    网状物。配置:

    <?xml version="1.0"?>
    
        <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
          <configSections>
              <section name="MyCustomSection" 
                       type="MyNamespace.MyCustomSectionHandler, MyAssembly"/>
        </configSections>
    <configProtectedData>
        <providers>
          <clear />
          <add name="DataProtectionConfigurationProvider"
               type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,
                       Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,
                       processorArchitecture=MSIL"
               keyContainerName="MyKeyContainer"
               useMachineContainer="true" />
        </providers>
      </configProtectedData>
        <MyCustomSection>
           <blah name="blah1">
              <blahChild name="blah1Child1" />
           </blah>
        </MyCustomSection>
    

    在尝试加密之前,配置处理程序工作得很好。当我尝试用以下方式加密它时:

    aspnet_regiis-pef“MyCustomSection” 数据保护配置提供程序

    我收到一个错误:

    配置节处理程序 依赖性。系统找不到 指定的文件。

    我已尝试配置/不配置提供程序。有/没有分组。有/没有事先启动网站。我已尝试暂时将我的程序集放入GAC进行注册。我还尝试了我的log4net部分,只是为了尝试一些不属于我的东西,但没有成功。我以管理员身份运行了命令提示符。有什么想法吗?或者ASPNet_RegIIS不能用于自定义节吗?

    MSDN 正在将我的处理程序更改为从ConfigurationSection继承,而不是实现IConfigurationSectionHandler,因为它在2.0中在技术上已被弃用(希望它与aspnet_regiis版本有关)。那里也没有运气。

    5 回复  |  直到 17 年前
        1
  •  38
  •   hwiechers    14 年前

    aspnet_regiis 必须能够绑定组件。正常的.net绑定规则适用。

    我通过创建名为 aspnet_regiis_bin 与位于同一目录中 aspnet_regiis.exe 和a aspnet_regiis.exe.config 提交 aspnet_regiis_bin

    <configuration>
       <runtime>
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
             <probing privatePath="aspnet_regiis_bin"/>
          </assemblyBinding>
       </runtime>
    </configuration>
    

    然后,我将定义自定义配置部分的程序集复制到 aspnet_regiis_bin aspnet_regiis 可以找到他们。

        2
  •  19
  •   Mathew Paxinos    12 年前

    <configSection>
        <!--
        <section name="CustomSection" type="" />
        -->
    </configSection>
    

    然后,您可以使用以下命令运行加密 aspnet_regiis -pef 像往常一样。运行后,只需取消注释该部分,您的网站即可运行。

        3
  •  4
  •   AJ.    17 年前

    这是一个彻底的破解,但我不确定是否有另一种方法可以在不强烈命名定义自定义部分的程序集并对其进行GAC化的情况下做到这一点(尽管你提到这也不起作用,我不知道为什么它不起作用)。由于aspnet_regiis在<驱动器>:\Windows\Microsoft。Net\Framework\<版本>文件夹(在WinXP中),您可以将定义配置部分的DLL复制到相关框架中;版本>文件夹,然后它应该工作。

        4
  •  4
  •   Community Mohan Dere    8 年前

    为了记录在案,我最后用了一个小的维护页面来为我做这件事。

    var currentConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");
    // Unprotect
    ConfigurationSection section = currentConfig.GetSection("MyCustomSection");
    if (section.SectionInformation.IsProtected)
    {
       section.SectionInformation.UnprotectSection();
       currentConfig.Save();
    }
    
    // Protect
    if (!section.SectionInformation.IsProtected)
    {
         section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
         currentConfig.Save();
    }
    

    警告:您的进程需要对正在修改的配置文件具有写访问权限。你需要某种方式来授权谁可以运行这个。你会 generally

        5
  •  2
  •   Philippe    14 年前

    显示为正确的答案是正确的。我想添加注释,但无法添加,因为注释太长(示例配置条目)。

    节名称应使用程序集的全名。运行时程序集限定不适用于aspnet_regiis.exe。

    <configSections>
      <section name="securityConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Configuration.SecuritySettings, Microsoft.Practices.EnterpriseLibrary.Security, Version=5.0.414.0, Culture=neutral, PublicKeyToken=9c844884b2afcb9e" />
    </configSections>
    

    但这行不通:

    <configSections>
      <section name="securityConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Configuration.SecuritySettings, Microsoft.Practices.EnterpriseLibrary.Security" />
    </configSections>
    
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <qualifyAssembly partialName="Microsoft.Practices.EnterpriseLibrary.Security" fullName="Microsoft.Practices.EnterpriseLibrary.Security, Version=5.0.414.0, Culture=neutral, PublicKeyToken=9c844884b2afcb9e" />
        </assemblyBinding>
    </runtime>
    
    推荐文章