Encrypting custom sections of a web.config

2019-03-15 10:01发布

问题:

I used the article Creating a Flexible Configuration Section Handler to create a Flexible Configuration Section Handler in my application.

I also saw this article entitled Encrypting Custom Configuration Sections on the OdeToCode blog, on how to encrypt portions of a web.config file.

From the first article, we have this web.config code.

<?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>

I tried to use the following code to encrypt the code using something like the following command line code.

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

I am getting the following error

Could not load type FifteenSeconds.Core.BasicConfigurator' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Any help would be appreciated.

回答1:

Here's another workaround for this issue (found at http://www.dotnetnoob.com/2013/01/how-to-encrypt-custom-configuration.html). Comment out the section element for the custom section under the configSections element (/configuration/configSections) before running the aspnet_regiis command and the custom section should get encrypted.

<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!


回答2:

The only known solution is a terrible hack. Copy the assembly (and all dependencies) to the relevant .NET framework directory (where aspnet_regiis.exe is located).



回答3:

trying change type to include the assembly name

type="FifteenSeconds.Core.BasicConfigurator, MyWebApplication"

This assumes the BasicConfiguration is in your Web App



回答4:

I've had a similar problem when referencing a type in my configuration file. As Conrad Frix suggested, you need a reference to the assembly name after the namespace type reference. I have made the mistake of putting down what I think the assembly name is rather than checking it may have a different name to the name of the project. You can make sure by right clicking on the project in Visual Studio and going to properties. Double check to make sure that the project is outputting an assembly with the same name as you are specifying in your web.config.



回答5:

Something like this might work, i havent tried it myself and not a clean solution

http://blogs.msdn.com/b/kaevans/archive/2004/08/19/217177.aspx which uses System.Configuration.NameValueSectionHandler.

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

I have tried this way though, using System.Configuration.SingleTagSectionHandler and

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

http://vaultofthoughts.net/UsingSingleTagSectionHandlerInsteadOfAppSettings.aspx



回答6:

I just resolved a similar issue very easily. You need to specify the library within the "type" attribute.

Instead of:

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

Try:

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

My issue was almost the exact same, although I was using the .NET libraries instead.

This:

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

Became:

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

Hopefully this works.