简体   繁体   中英

Updating the Web.config Encrypted Connstring

Having an issue when web.config using the code:

Configuration config = ConfigurationManager.OpenExeConfiguration(webConfigFile);

        ConfigurationSection constring_section = config.GetSection(section);
        if (constring_section != null && !constring_section.SectionInformation.IsProtected)
        {
            constring_section.SectionInformation.ProtectSection(provider);
            config.Save();

        }
    }

The code works however, instead of updating the actual "web.config"(value of webConfigFile), it creates "web.config.config" and it only contains the connectionStrings section:

<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
        xmlns="http://www.w3.org/2001/04/xmlenc#">
        <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" />
        <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
                <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
                <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                    <KeyName>Rsa Key</KeyName>
                </KeyInfo>
                <CipherData>
                    <CipherValue>b4PM97Ct8+3R2/tWe0sz1knTlcmlNrT7veu/H9fS/pzX6Hou6EK8A6vQoNhzHcxE44CvYmihw2mlP02sHd61AsEthSwY5OHkdnzvgE119vIdxYpiHJuNIkv2R3wNgr0XkxLQ5irvc4uywPHTF/Mmk/FV1xxX7AOkAr3lhJzASSxcAbW4F5xS47dViRv7nyU6jmuMQvL3oRGNWjDLTwx5mmJtfbbghWrmL+Rnu5AB5CyFv98QG9QlZ84ePlzuPPcZEa885iSHlw4MOoUAhtPVIsH7E6JvB59ovkZciWADLOJ+jbAJrfHvT0vwKbyJtDSk9yFj9iv2CADTL9GjqxdyBw==</CipherValue>
                </CipherData>
            </EncryptedKey>
        </KeyInfo>
        <CipherData>
            <CipherValue>DbC8NhnEszhEGf2/D6FIqhoz+aL8yW0yKkKHLPpAxkLVCwj7hX3SuVMKwBdRi1me</CipherValue>
        </CipherData>
    </EncryptedData>
</connectionStrings>

Can someone shed a light

Found out the solution,

It's about the usage of OpenExeConfiguration vs. OpenMappedExeConfiguration

OpenMappedExeConfiguration vs. OpenExeConfiguration

so from:

Configuration config = ConfigurationManager.OpenExeConfiguration(webConfigFile);

    ConfigurationSection constring_section = config.GetSection(section);
    if (constring_section != null && !constring_section.SectionInformation.IsProtected)
    {
        constring_section.SectionInformation.ProtectSection(provider);
        config.Save();

    }
}

to

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = webConfigFile} , ConfigurationUserLevel.None);
        ConfigurationSection constring_section = config.GetSection(section);
        if (constring_section != null && !constring_section.SectionInformation.IsProtected)
        {
            constring_section.SectionInformation.ProtectSection(provider);
            config.Save();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM