Posts

Showing posts from July, 2012

Automatically encrypt your ASP.NET Web.Config sections at runtime

Image
  Today I will show you how to encrypt the web.config settings in your production server at runtime. Take a look at the EncryptConfig snippet below.  This will encrypt the connectionStrings, system.web/sessionState, and appSettings section of the web.config Private Sub EncryptConfig() '1. Open the Web.Config for editing Dim cfg As System.Configuration.Configuration = _ System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath) '2. Add the configuration sections that you want to encrypt Dim lSections As New Generic.List(Of String) With lSections .Add("connectionStrings") .Add("system.web/sessionState") .Add("appSettings") End With '3. Iterate each sections in the list that we added and encrypt it using DataProtectionConfigurationProvider Dim section As Confi