Tuesday, December 22, 2009

Protect/Encrypt config file in c#

Assume that we have web.config or app.config file like this:
XML:
<?xml version="1.0" encoding="utf-8"?>
>
>
name="AdventureData" connectionString="adventuredb"
providerName="System.Data.SqlClient" />

>
>
key="UserName" value="Vijay" />
key="password" value="dhiraj" />
key="dbIP" value="dbAdventure"/>
>
>

example :- ProtectSection("appSettings","DataProtectionConfigurationProvider");
private void ProtectSection(string sectionName,
string provider) { Configuration config = WebConfigurationManager. OpenWebConfiguration(Request.ApplicationPath); ConfigurationSection section =
config.GetSection(sectionName); if (section != null &&
!section.SectionInformation.IsProtected) { section.SectionInformation.ProtectSection(provider); config.Save(); } }
private void UnProtectSection(string sectionName) {     Configuration config =         WebConfigurationManager.             OpenWebConfiguration(Request.ApplicationPath);      ConfigurationSection section =
config.GetSection(sectionName); if (section != null &&
section.SectionInformation.IsProtected) { section.SectionInformation.UnprotectSection(); config.Save(); } }


No comments:

Post a Comment