首页 文章

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

提问于
浏览
28

我有一个带有自定义配置部分的Web应用程序 . 该部分包含我想要加密的信息(希望使用ASPNet_RegIIS而不是自己做) .

Web.Config中:

<?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”c:\ inetpub \ wwwroot \ MyWebsite -prov DataProtectionConfigurationProvider

我收到一个错误:

加密配置部分...为MyCustomSection创建配置节处理程序时出错:无法加载文件或程序集“MyAssembly”或其依赖项之一 . 该系统找不到指定的文件 . (c:\ inetpub \ wwwroot \ MyWebsite \ web.config第5行)

我已尝试使用/不配置提供程序 . 有/无部分组 . 有/没有事先启动网站 . 我已经尝试暂时将我的程序集放入GAC进行注册 . 我也试过我的log4net部分只是尝试一些不是我的东西,没有运气 . 我以管理员身份运行命令提示符 . 有任何想法吗?或者ASPNet_RegIIS是否可以不用于自定义部分?

查看MSDN之后的最后一个镜头是将我的处理程序更改为继承ConfigurationSection而不是实现IConfigurationSectionHandler,因为它在2.0中被技术上弃用(希望它是关于aspnet_regiis版本的东西) . 也没有运气 .

任何想法让我知道 . 谢谢!

5 回答

  • 3

    显示正确的答案是正确的 . 我想添加评论,但不能,因为这是一个评论太长(示例配置条目) .

    部分名称应使用程序集的全名 . 运行时程序集资格认证不适用于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>
    
  • 15

    aspnet_regiis 必须能够绑定程序集 . 正常的.net绑定规则适用 .

    我通过在与 aspnet_regiis.exe 相同的目录中创建名为 aspnet_regiis_bin 的目录和使用 aspnet_regiis_bin 作为私有路径的 aspnet_regiis.exe.config 文件来解决此问题,如下所示:

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

    然后,我将定义自定义配置节的程序集复制到 aspnet_regiis_bin ,以便 aspnet_regiis 可以找到它们 .

    此过程不要求程序集强名称或在GAC中,但需要在框架目录中进行处理 .

  • 4

    为了记录,我最终得到了一个维护页面来为我做这个 .

    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();
    }
    

    注意事项:您的进程需要对正在修改的配置文件进行写访问 . 您保存时重新启动网站'll want some way to authorize who can run this. You' ll generally .

  • 2

    我正在使用一种解决方法,我暂时注释掉configSections元素的内容:

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

    然后,您可以像往常一样使用 aspnet_regiis -pef 运行加密 . 在运行之后,只需取消注释该部分,您的站点就可以运行了 .

  • 30

    这是一个完全黑客攻击,但是我不确定是否有另一种方法可以在没有强烈命名定义自定义部分和GAC化它的程序集的情况下执行它(尽管你提到它也不起作用,我也不确定为什么它不会) . 由于aspnet_regiis在<drive>:\ Windows \ Microsoft.Net \ Framework \ <version>文件夹(在WinXP中)中运行,因此您可以将定义配置节的DLL复制到相关的Framework \ <version>文件夹中,然后将其复制到应该管用 .

相关问题