///
<summary>
///
web.config 加密
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected
void
EncryptConnStrings_Click(
object
sender, EventArgs e) {
//
Get configuration information about Web.config
Configuration config
=
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
//
Let's work with the <connectionStrings> section
ConfigurationSection connectionStrings
=
config.GetSection(
"
connectionStrings
"
);
if
(connectionStrings
!=
null
)
//
Only encrypt the section if it is not already protected
if
(
!
connectionStrings.SectionInformation.IsProtected) {
//
Encrypt the <connectionStrings> section using the DataProtectionConfigurationProvider provider
connectionStrings.SectionInformation.ProtectSection(
"
DataProtectionConfigurationProvider
"
); config.Save();
//
Refresh the Web.config display
DisplayWebConfig(); } }
///
<summary>
///
web.config 解密
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected
void
DecryptConnStrings_Click(
object
sender, EventArgs e) {
//
Get configuration information about Web.config
Configuration config
=
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
//
Let's work with the <connectionStrings> section
ConfigurationSection connectionStrings
=
config.GetSection(
"
connectionStrings
"
);
if
(connectionStrings
!=
null
)
//
Only decrypt the section if it is protected
if
(connectionStrings.SectionInformation.IsProtected) {
//
Decrypt the <connectionStrings> section
connectionStrings.SectionInformation.UnprotectSection(); config.Save();
//
Refresh the Web.config display
DisplayWebConfig(); } }
转载于:https://www.cnblogs.com/spark_wu/archive/2010/03/09/1681402.html
相关资源:C# Web.Config 加密与解密
转载请注明原文地址: https://mac.8miu.com/read-154847.html