I've done the following in the Web.config
<configuration>
<configSections>
<section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" allowLocation="true" />
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
... etc
And then in the Views/Web.config I've tried to use it like this
<secureAppSettings>
<!--Captcha keys-->
<add key="RecaptchaPrivateKey" value="somevalue" />
<add key="RecaptchaPublicKey" value="someothervalue" />
</secureAppSettings>
however every time try to open the site I get the following error:
Config Error The configuration section 'secureAppSettings' cannot be read because it is missing a section declaration
and I don't understand what I'm supposed to do. I'm currently trying to use it in the view like this:
@((NameValueCollection)WebConfigurationManager.GetSection("secureAppSettings"))["RecaptchaPublicKey"]
Edit:
I've tried as one of the comments suggest to move the configSections to the View/web.config
the result is the following Error message
Parser Error Message: An error occurred while creating the handler for the secureAppSettings configuration section: The specified assembly name or specified codebase was invalid. (Exception from HRESULT: 0x80131047)
I took that as a sign that I was doing it wrong but I see now that maybe I need to fix something here instead of doing it the other way I was doing.
however the message for me is as cryptic as before..
Clarification of what I'm trying to archive.
I've got some sensitive information in my Web.config that I would like to encrypt. I read that I could make a secureAppSettings section and then encrypt it from there. the reason I want to separate the information out of the appSettings section is because I've got plenty of other information in it that I really do not need encrypted. eventually all of this should lead to that I'm able to make a MSI website installer where, during the installation, I can change or add settings to the appSettings and the secureAppSettings sections before encrypting the secureAppSettings. for easy setup and installation of the site I'm building.
Now I'm not sure if the way my solution is currently, is going to work, but if not, then I'd like to be pointed in the right direction, to solve my ultimate goal, of being able to make a MSI installer, where I can change the configuration file, during the installation and encrypt any sensitive information.
In reply to my comment you mentioned you also tried a setup using the root web.config
file, instead of the one in the Views
folder.
In theory it must be possible to use the Views\web.config
file.
With ASP.NET MVC
I only have success using the root web.config
.
This looks like here below.
Notice that I use a less explicit section
definition.
Web.config
<configuration>
<configSections>
<section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<!-- Other configuration elements -->
<secureAppSettings>
<!--Captcha keys-->
<add key="RecaptchaPrivateKey" value="somevalue" />
<add key="RecaptchaPublicKey" value="someothervalue" />
</secureAppSettings>
</configuration>
In my view
<span>@(((NameValueCollection)WebConfigurationManager.GetSection("secureAppSettings"))["RecaptchaPublicKey"])</span>