Encrypt Connection Strings on Application Startup

Just so I don’t forget how I do this:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
protected void Application_Start()
{
#if !DEBUG
EncryptConnectionStrings();
#endif
}
 
private void EncryptConnectionStrings()
{
var sectionName = "connectionStrings";
var config = WebConfigurationManager.OpenWebConfiguration("~");
 
ConfigurationSection section = config.GetSection(sectionName);
 
if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.Save();
}
}
view raw gistfile1.cs This Gist brought to you by GitHub.
Posted in .net

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>