Asp.net

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

  • April 24, 2009

我有一個帶有自定義配置部分的 Web 應用程序。該部分包含我想加密的資訊(希望使用 ASPNet_RegIIS 而不是自己做)。

網路配置:

<?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_royal -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 版本有關)。那裡也沒有運氣。

任何想法讓我知道。謝謝!

aspnet_regiis必須能夠綁定程序集。正常的 .net 綁定規則適用。

我通過創建aspnet_regiis_bin在同一目錄中 呼叫的目錄aspnet_regiis.exe和一個具有如下私有路徑的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可以找到它們。

此過程不要求程序集是強命名的或在 GAC 中,但確實需要在框架目錄中搞亂。

引用自:https://stackoverflow.com/questions/786661