Home »

Ever needed to quickly Encrypt the webconfig file?

6. October 2008 by thebeebs 7 Comments
If you’ve ever need to encrypt your web config file here’s a quick code snippet that you might find useful: 
   1: Namespace Einstein.Core.Security.Config
   2:  
   3:     <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeSealed")> Public Class Settings
   4:         Private Sub New()
   5:         End Sub
   6:         Public Shared Sub ProtectSection(ByVal sectionName As String, ByVal provider As String)
   7:             Dim config As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath())
   8:             Dim section As ConfigurationSection = config.GetSection(sectionName)
   9:             If Not (section Is Nothing) AndAlso Not section.SectionInformation.IsProtected Then
  10:                 section.SectionInformation.ProtectSection(provider)
  11:                 config.Save()
  12:             End If
  13:         End Sub
  14:  
  15:  
  16:         Public Shared Sub UNProtectSection(ByVal sectionName As String)
  17:             Dim config As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath())
  18:             Dim section As ConfigurationSection = config.GetSection(sectionName)
  19:             If Not (section Is Nothing) AndAlso section.SectionInformation.IsProtected Then
  20:                 section.SectionInformation.UnprotectSection()
  21:                 config.Save()
  22:             End If
  23:         End Sub
  24:     End Class
  25: End Namespace

   1: namespace Einstein.Core.Security.Config
   2: {
   3:  
   4:     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1052:StaticHolderTypesShouldBeSealed")]
   5:     public class Settings
   6:     {
   7:         private Settings()
   8:         {
   9:         }
  10:         public static void ProtectSection(string sectionName, string provider)
  11:         {
  12:             System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath());
  13:             ConfigurationSection section = config.GetSection(sectionName);
  14:             if (!(section == null) && !section.SectionInformation.IsProtected)
  15:             {
  16:                 section.SectionInformation.ProtectSection(provider);
  17:                 config.Save();
  18:             }
  19:         }
  20:  
  21:  
  22:         public static void UNProtectSection(string sectionName)
  23:         {
  24:             System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath());
  25:             ConfigurationSection section = config.GetSection(sectionName);
  26:             if (!(section == null) && section.SectionInformation.IsProtected)
  27:             {
  28:                 section.SectionInformation.UnprotectSection();
  29:                 config.Save();
  30:             }
  31:         }
  32:     }
  33: }

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading