Monday, December 21, 2009

Encrypting Connection Strings in web.config file using Code

//Runthis method in page load

public void EncryptConnString()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (section.SectionInformation.IsProtected)
{
//To Encript
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
config.Save();

//To Decript
//section.SectionInformation.UnprotectSection();
//config.Save();
}
}

No comments: