Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新Web.config加密字符串_C#_Web Config - Fatal编程技术网

C# 更新Web.config加密字符串

C# 更新Web.config加密字符串,c#,web-config,C#,Web Config,web.config使用代码时出现问题: Configuration config = ConfigurationManager.OpenExeConfiguration(webConfigFile); ConfigurationSection constring_section = config.GetSection(section); if (constring_section != null && !constring_section.Se

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”,并且只包含ConnectionString部分:

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

Rsa密钥
2)一个2(2)两个2(2)两个2(2)两个2(2)两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两个两ADTL9GJQXDYBW==
DbC8NhnEszhEGf2/D6FIqhoz+aL8yW0yKkKHLPpAxkLVCwj7hX3SuVMKwBdRi1me

有人能解释一下吗

找到了解决办法

这是关于OpenExeConfiguration与OpenMappedExeConfiguration的使用

因此,来自:

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

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