28

我有一個自定義配置部分的Web應用程序。該部分包含我想要加密的信息(希望使用ASPNet_RegIIS而不是自己做)。使用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

我得到一個錯誤:

Encrypting configuration section... An error occurred creating the configuration section handler for MyCustomSection: Could not load file or assembly 'MyAssembly' or one of its dependencies. The system cannot find the file specified. (c:\inetpub\wwwroot\MyWebsite\web.config line 5)

我已經試過有/無配置的提供者。有/無部分組。有/未事先啓動網站。我已經嘗試暫時將我的程序集放入GAC中進行註冊。我也試過我的log4net部分,只是爲了嘗試一些不屬於我的東西,沒有運氣。我以管理員身份運行命令提示符。有任何想法嗎?或者ASPNet_RegIIS只能用於自定義部分?

查看MSDN後最後一個鏡頭正在改變我的處理程序從ConfigurationSection繼承,而不是實現IConfigurationSectionHandler,因爲它在2.0技術上被棄用(希望它是關於aspnet_regiis版本)。那裏也沒有運氣。

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

+0

我得到了同樣的問題。我不認爲有沒有辦法讓這個工作沒有將程序集放在gac中或在下面的答案中進行破解? – 2009-07-16 16:24:38

+0

我厭倦了它的擺弄 - 所以我只是暫時把組裝放在gac中去了。 – 2009-07-16 21:23:02

回答

29

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

我解決這個問題通過創建在同一目錄下名爲aspnet_regiis_bin目錄aspnet_regiis.exeaspnet_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中,但確實需要在框架目錄中進行調整。

4

這是一個徹頭徹尾的破解,但我不確定還有另一種方法可以做到這一點,如果沒有強制命名定義您的自定義部分並對其進行GAC化的程序集(儘管您提到該程序無效,但我不知道爲什麼它不會)。由於aspnet_regiis在<驅動器>:\ Windows \ Microsoft.Net \ Framework \ <版本>文件夾(在WinXP中)中運行,因此可以將定義配置節的DLL複製到相關的Framework \ <版本>文件夾中,應該管用。

+1

當我將它移入GAC時,它的名字很有名。我還確保在我的配置/ section.type中包含版本/文化/公鑰。雖然你的解決方案確實讓我通過,謝謝。 如果其他人遇到過這個問題,我最終將dll移動到框架文件夾中,然後以管理員身份運行aspnet_regiis(Windows Server 2007)。 – 2009-04-24 20:40:38

+0

經過更多的調整之後,我從GAC註冊了它。我從未嘗試向GAC註冊並同時使用該類型的長格式(儘管我在不同的時間都使用了這兩種格式)。思考完之後,組委會需要加入GAC是非常合理的。再次感謝。 – 2009-04-24 21:21:55

2

顯示爲正確的答案是正確的。我想添加評論,但不能,因爲這是太長的評論(樣本配置條目)。

部分名稱應使用程序集的全名。運行時程序集資格不適用於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> 
13

我使用的一種變通方法,由此我即暫時註釋掉configSections元素的內容:

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

然後您可以照常使用aspnet_regiis -pef運行加密。運行後,只需取消該部分的註釋並且您的站點已準備好運行。

3

爲了記錄,我結束了一個小維護頁面爲我做這個。

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重新啓動網站。