aspnet / MicrosoftConfigurationBuilders

Microsoft.Configuration.Builders
MIT License
118 stars 61 forks source link

Not working in IIS #241

Open RichardD2 opened 4 weeks ago

RichardD2 commented 4 weeks ago

I'm trying to use this library to keep credentials out of my web.config file for an MVC5 application.

Rather than messing around in the systemprofile folder, which is where IIS seems to look for its AppData folder, I've set the project up to load the secrets file from the application root:

web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
    </configSections>

    <configBuilders>
        <builders>
            <add name="Secrets" mode="Token" userSecretsFile="~/web.secrets.config" type="Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        </builders>
    </configBuilders>

    <appSettings>
        <add key="MySecretSetting" value="${MySecretSetting}" />
    </appSettings>

    ...
</configuration>

web.secrets.config:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <secrets ver="1.0">
        <secret name="MySecretSetting" value="Super secret setting" />  
    </secrets>
</root>

When I run this locally in IIS on my Windows 11 PC, it works as expected. Reading WebConfigurationManager.AppSettings["MySecretSetting"] returns "Super secret setting".

When I deploy this to IIS on a Windows Server 2016 computer, it does not work. The setting returns the placeholder value: "${MySecretSetting}".

Using Process Monitor, I can see that w3wp.exe successfully opens ~/bin/Microsoft.Configuration.ConfigurationBuilders.UserSecrets.dll. However, it makes absolutely no attempt to open the secrets file. There isn't even a "not found" or "access denied" entry to start to diagnose what the problem is; it just doesn't seem to do anything.

Both machines have .NET Framework 4.8 installed, so as far as I can see, this library should be supported.

Am I missing something obvious?