Mister 832
Mister 832

Reputation: 1221

Encrypt custom section in app.config

I want to encrypt the following section in my c# app.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="ConX" type="System.Configuration.NameValueSectionHandler" />
  </configSections>

  <ConX>
    <add key="SqlSrv" value="0.0.0.0"/>
  </ConX>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
...

The following Code doesn't work, because the NameValueCollection does not support the ProtectSection Method.

How can I encrypt this section?

var section = ConfigurationManager.GetSection("ConX") as NameValueCollection;
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

When I try the following line, the section object just stays null

AppSettingsSection section = ConfigurationManager.GetSection("ConX") as AppSettingsSection;

However, if i read the value like shown below it works, so the section can be found.

var section = ConfigurationManager.GetSection("ConX") as NameValueCollection;
var value = section["SqlSrv"];

Upvotes: 1

Views: 1019

Answers (1)

toepoke.co.uk
toepoke.co.uk

Reputation: 827

I've just implemented the same thing for my website this week, you may find the following code useful.

Kind regards.

/// <summary>
/// Encrypts one or more sections of the web.config using the provided provider.
/// </summary>
/// <param name="protectionProvider">
/// Protection provider to use:
/// RsaProtectedConfigurationProvider or DPAPIProtectedConfigurationProvider.
/// </param>
/// <param name="sectionsToEncrypt">Array of section names to encrypt</param>
/// <returns>
/// On success returns true
/// On failure returns false
/// </returns>
public static bool EncryptConfigurationSections(
    string protectionProvider, 
    params string[] sectionsToEncrypt
) {
    bool isOK = true;
    List<string> SUPPORTED_PROVIDERS = new List<string>() { 
        "RsaProtectedConfigurationProvider", 
        "DPAPIProtectedConfigurationProvider" 
    };

    if (!SUPPORTED_PROVIDERS.Contains(protectionProvider)) {
        throw new ArgumentException("Provided provider is not supported.", "protectionProvider");
    }

    try {
        Configuration webConfiguration = null;
        bool saveRequired = false;
        // OpenWebConfiguration call will find the web.config file, we only need the directory (~)
        webConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

        // Protect all specified sections 
        // ... Do all that apply in one go so we only have the hit of saving once
        foreach (string sectionToEncrypt in sectionsToEncrypt) {
            ConfigurationSection configSection = webConfiguration.GetSection(sectionToEncrypt);

            // No point encrypting if it's already been done
            if (configSection != null && !configSection.SectionInformation.IsProtected) {
                saveRequired = true;
                configSection.SectionInformation.ProtectSection(protectionProvider);
                configSection.SectionInformation.ForceSave = true;
            }
        }

        if (saveRequired) {
            // Only save if there's a section which was not protected
            // ... again, no point taking the hit if we don't need to
            webConfiguration.Save(ConfigurationSaveMode.Modified);
        }
    }
    catch (Exception e) {
        isOK = false;
    }

    return isOK;

} // EncryptConfigurationSections

Upvotes: 1

Related Questions