Tuesday, January 15, 2008

Encrypt / Decrypt Section in Configuration File

Lets understand Encrypt and Decrypt particular section in web.config file with example of connectionstring encrypt in web.config file.

Add Namespace

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:

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape