Lets understand Encrypt and Decrypt particular section in web.config file with example of connectionstring encrypt in web.config file.
using System.Web.Configuration;
How to Encrypt Connection String in Asp.net stored in Web.Config file
protected void Button1_Click(object sender, EventArgs e)
{
String webConfigPath = “~”;
Configuration config = WebConfigurationManager.OpenWebConfiguration(webConfigPath);
ConfigurationSection configSection =
config.GetSection("connectionStrings");
configSection.SectionInformation.ProtectSection
("DataProtectionConfigurationProvider");
config.Save();
}
Understanding code:
1. Defining Path for Web.Config.
2. Opening Web Configuration File.
3. Getting Connection String Section for Encryption.
4. Protect Connection String Section.
5. Make Changes Saved to Configuration File.
How to Decrypt Connection String in Asp.net stored in Web.Config file
protected void Button1_Click(object sender, EventArgs e)
{
String webConfigPath = “~”;
Configuration config = WebConfigurationManager.OpenWebConfiguration(webConfigPath);
ConfigurationSection configSection =
config.GetSection("connectionStrings");
configSection.SectionInformation.UnprotectSection();
config.Save();
}
No comments:
Post a Comment