简体   繁体   English

更新 Web.config 加密连接字符串

[英]Updating the Web.config Encrypted Connstring

Having an issue when web.config using the code:使用代码 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();

        }
    }

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:然而,该代码有效,而不是更新实际的“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>

Can someone shed a light有人可以发光吗

Found out the solution,找到了解决办法,

It's about the usage of OpenExeConfiguration vs. OpenMappedExeConfiguration这是关于 OpenExeConfiguration 与 OpenMappedExeConfiguration 的使用

OpenMappedExeConfiguration vs. OpenExeConfiguration OpenMappedExeConfiguration 与 OpenExeConfiguration

so from:所以从:

Configuration config = ConfigurationManager.OpenExeConfiguration(webConfigFile);配置配置 = 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();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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