更新 Web.config 加密的 Connstring
Updating the Web.config Encrypted Connstring
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>
谁能帮我点灯
找到解决方案,
这是关于 OpenExeConfiguration 与 OpenMappedExeConfiguration 的用法
OpenMappedExeConfiguration vs. OpenExeConfiguration
所以来自:
配置 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();
}
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>
谁能帮我点灯
找到解决方案,
这是关于 OpenExeConfiguration 与 OpenMappedExeConfiguration 的用法
OpenMappedExeConfiguration vs. OpenExeConfiguration
所以来自:
配置 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();
}