This project is read-only.

The provider is plugged in exactly like it was a ProtectedConfigurationProvider. It must be declared in Web.Config, and then appropriate sections where by-host runtime configuration is required must be changed to use the provider.

To declare the use of the provider:

  <configProtectedData defaultProvider="ByHostProvider">
    <providers>
      <add name="ByHostProvider"
           type="Axantum.Xecrets.Providers.ByHostProtectedConfigurationProvider, ByHostProtectedConfigurationProvider, Version=1.0.1.0, Culture=neutral, PublicKeyToken=93aa1dff88e93343" />
    </providers>
  </configProtectedData>

To use it for regular settings, the following is a typical sample:

<system.net>
  <mailSettings>
    <smtp configProtectionProvider="ByHostProvider">
      <EncryptedData>
        <!-- Default settings -->
        <host>
          <smtp from="Axantum Software AB [No Reply] &lt;software@axantum.com&gt;">
            <network host="127.0.0.1"
                     password=""
                     port="25"
                     userName="" />
          </smtp>
        </host>
        <!-- Development PC -->
        <host hostName="MyDevPC">
          <smtp>
            <network host="smtp.example.net"
                     password=""
                     port="25"
                     userName="" />
          </smtp>
        </host>
        <!-- Production -->
        <host hostName="production">
          <smtp>
            <network host="1.2.3.4"
                     password="password"
                     port="25"
                     userName="user" />
          </smtp>
        </host>
      </EncryptedData>
    </smtp>
  </mailSettings>
</system.net>

There is also a special mode for 'add', 'remove', 'clear' sections, like this:

<appSettings configProtectionProvider="ByHostProvider">
  <EncryptedData>
    <!--Default settings-->
    <host>
      <appSettings>
        <add key="Currency"
             value="SEK" />
        <add key="VatPercent"
             value="25" />
        <add key="IsProductionEnvironment"
             value="true" />
        <add key="SupportsSsl"
             value="true" />
      </appSettings>
    </host>
    <host hostName="MyDevPC"
          mode="arc">
      <appSettings>
        <add key="IsProductionEnvironment"
             value="false" />
        <add key="SupportsSsl"
             value="false" />
      </appSettings>
    </host>
  </EncryptedData>
</appSettings>

This makes it possible to maintain a single Web.Config file for all environments and have it select the right configuration values depending on the hostname at runtime. Starting with Visual Studio 2012, the Web.Config transform functionality is actually useful, so it may be a better solution in many cases. However this model still has the advantage of not requiring many configurations in your projects. In fact, a combination of both may be the right model in some cases.

Last edited Mar 29, 2013 at 5:44 PM by svante, version 3

Comments

No comments yet.