Search

Saturday, January 24, 2009

Web.confing Connection String Encrypted Decrypted



Web.config Encyrpted and Decrypted






protected void Page_Load(object sender, EventArgs e)

{

// load web.config into TextBox on first page visit...

if (!Page.IsPostBack)

{

RefreshWebConfig();

}

}



private void RefreshWebConfig()

{

// Read in the value of Web.config into the TextBox...

using (StreamReader reader = File.OpenText(Server.MapPath("~/Web.config")))

{

webConfig.Text = reader.ReadToEnd();

}



// Programmatically read in the value of the connection string

PlainTextConnectionString.Text =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ConnectionString;

}



protected void btnRefreshWebConfig_Click(object sender, EventArgs e)

{

RefreshWebConfig();

}



protected void btnEncrypt_Click(object sender, EventArgs e)

{

ProtectSection("connectionStrings", "DataProtectionConfigurationProvider");

RefreshWebConfig();

}



protected void btnDescrypt_Click(object sender, EventArgs e)

{

UnProtectSection("connectionStrings");

RefreshWebConfig();

}



protected void btnEncAuthentication_Click(object sender, EventArgs e)

{

ProtectSection("system.web/authentication", "DataProtectionConfigurationProvider");

RefreshWebConfig();

}



protected void btnDecAuthentication_Click(object sender, EventArgs e)

{

UnProtectSection("system.web/authentication");

RefreshWebConfig();

}



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

}

}






Blog Archive

Contributors