I have a problem (or at least I think I do). I am attempting to set the machine key of a site in it’s web.config in order to prepare for future sharing of the forms authentication data between sites. I first set the following in the web.config:
<machineKey validationKey="<SOME_VALUE>" decryptionKey="<SOME_VALUE>" validation="SHA1" decryption="AES"/>
As a test, I wrote the following to test new machineKey element in the web.config:
var machineConfigMachineKey = (MachineKeySection)WebConfigurationManager.OpenMachineConfiguration().SectionGroups["system.web"].Sections["machineKey"];
var webConfigMachineKey = (MachineKeySection)WebConfigurationManager.OpenWebConfiguration("").SectionGroups["system.web"].Sections["machineKey"];
Response.Write("<pre>");
Response.Write("<b>machine.config decrypt: </b>" + machineConfigMachineKey.DecryptionKey + "<br />");
Response.Write("<b>web.config decrypt: </b>" + webConfigMachineKey.DecryptionKey + "<br />");
Response.Write("<br />");
Response.Write("<b>machine.config validate: </b>" + machineConfigMachineKey.ValidationKey + "<br />");
Response.Write("<b>web.config validate: </b>" + webConfigMachineKey.ValidationKey + "<br />");
Response.Write("</pre>");
Response.End();
… which results in this display:
machine.config decrypt: AutoGenerate,IsolateApps
web.config decrypt: AutoGenerate,IsolateApps
machine.config validate: AutoGenerate,IsolateApps
web.config validate: AutoGenerate,IsolateApps
Obviously I am super confused by this, as I was expecting to see the custom values from the new machineKey element in the web.config instead of “AutoGenerate,IsolateApps”.
Am I missing something here that should be brutally obvious to me?
Thanks 🙂
Use the static API
WebConfigurationManager.GetSection("system.web/machineKey")instead. It automatically performs the logic of traversing the config hierarchy, finding the most applicable one (generally ~/Web.config for the current application), and pulling out its specific values.