dcsimg
 

Submitted by: Srinath M S(srinath_ms@yahoo.com)

Friday Apr 21st 2017 by Srinath M S

Language: .NET, Expertise: Advanced — Learn to encrypt a config section using .Net's configuration providers.

var configFilePath = @"C:\Code\Classified\Configurations\app.config";
var sectionName = "appSettings";
EncryptConfigSection(configFilePath, sectionName);

private void EncryptConfigSection(string configFilePath, string sectionName)
{
   var configurationFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
   var configuration = ConfigurationManager.OpenMappedExeConfiguration(configurationFileMap,   ConfigurationUserLevel.None);
   var configSection = configuration.GetSection(sectionName);
   var configurationProvider = new RsaProtectedConfigurationProvider();
   if (!configSection.SectionInformation.IsLocked)
   {
      configSection.SectionInformation.ProtectSection(configurationProvider.CspProviderName);
   }
   configuration.Save();
} 
Home
Mobile Site | Full Site