David Hayden [MVP C#]

Sponsors

The Lounge

News

  • CodeBetter.Com Home

Other Links

Teas

Patterns & Practices

Florida .NET Developer

Book Reviews

Tampa ASP.NET MVC Developer Group

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Encrypting ConnectionStrings Programmatically in App.config

I wrote a post called Encrypt Connection Strings, AppSettings and Web.Config in ASP.NET 2.0 - Security Best Practices that discusses programmatically encrypting various sections of your web.config in ASP.NET 2.0.

Today a question came up about how to encrypt connection strings programmatically in your app.config. I won't pretend to be a windows developer, but the following code seemed to do the trick:

 

Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSection section = config.GetSection("connectionStrings"); if (section != null) { if (!section.IsReadOnly()) { section.SectionInformation.
ProtectSection(
"RsaProtectedConfigurationProvider"); section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } }

 

You can add more checks to verify it isn't encrypted already as well as use the DPAPI provider instead, but this gets across the idea.

If anyone knows of another or better way, I would love to hear it.

 

Additional Resources:

 


Posted 03-11-2006 2:55 PM by David Hayden

[Advertisement]

Comments

Jason Haley wrote Interesting Finds
on 03-12-2006 7:08 AM