Web.config 加密解密

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

 

posted @ 2010-03-09 10:48  三颗屎  阅读(203)  评论(0编辑  收藏  举报