更新Web.config加密的Connstring

问题描述 投票:0回答:1

使用代码在web.config上出现问题:

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();

        }
    }

然而,该代码有效,而不是更新实际的“ web.config”(webConfigFile的值),它创建了“ web.config.config”,并且仅包含connectionStrings部分:

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

有人可以照亮

c# web-config
1个回答
0
投票

找出解决方案,

关于OpenExeConfiguration与OpenMappedExeConfiguration的用法

OpenMappedExeConfiguration vs. OpenExeConfiguration

所以来自:

配置配置= 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();
        }
© www.soinside.com 2019 - 2024. All rights reserved.