加密app.config中的自定义部分

本文关键字:自定义部 app config 加密 | 更新日期: 2023-09-27 18:12:20

我想在我的c#应用程序中加密以下部分。

<?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>
...

下面的代码不起作用,因为NameValueCollection不支持ProtectSection方法。

如何加密此部分?

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

当我尝试下面一行时,section对象只是保持为空

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

但是,如果我像下面这样读取值,它就会工作,所以可以找到该节。

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

加密app.config中的自定义部分

这周我刚刚为我的网站实现了同样的事情,你可能会发现下面的代码很有用。

亲切的问候。

/// <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