Note that I am aware of this question:
Encrypting AppSettings in file external to Web.Config
And this is NOT a dupe. That question has an answer allowing the encryption of an external appSettings config file only if appSettings is linked using the configSource
attribute. I want to use the file
attribute. So, I have something like this in my Web.config:
<appSettings file="ExternalSettings.config">
<add key="InternalSetting" value="Test123" />
</appSettings>
... and an ExternalSettings.config file in the same directory with its own appSettings
section. When I run my web app, the two appSettings
contents are merged together. However, if I try to just encrypt my ExternalSettings.config file like this:
var webConfig = WebConfigurationManager.OpenWebConfiguration("~/ExternalSettings.config");
ConfigurationSection section = webConfig.GetSection("appSettings");
if (!section.SectionInformation.IsProtected) {
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
webConfig.Save();
}
I get a ConfigurationErrorsException
saying
"A configuration file cannot be created for the requested Configuration object."
(by the way, this code works fine for encrypting an appSettings
section that is just in Web.config)
Is there any way of encrypting the settings in my ExternalSettings.config file, but leaving the Web.Config ones unencrypted?