thebeebs | Ever needed to quickly Encrypt the webconfig file?
thebeebs
Zeroing the desk - Ignore the design
 
 

Ever needed to quickly Encrypt the webconfig file?

by thebeebs 7. October 2008 01:27
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: }

Tags:

Comments (10) -

Donald Hughes
Donald Hughes United States
10/26/2008 8:12:56 PM #

How do you put code into your blog?  I have BlogEngine.NET 1.4.5 too.  I have not yet figured this out and I really like the way yours looks.

thebeebs
thebeebs United Kingdom
10/27/2008 1:59:07 AM #

If you intall windows live writter and connect it to the blog then download the code block addin it generates nice code blocks like the ones above Smile

Donald Hughes
Donald Hughes United States
10/27/2008 7:57:26 AM #

Thanks, I'll give this "windows live writter" a try.

thebeebs
thebeebs United Kingdom
10/27/2008 11:49:12 AM #

You'll love it... Comment back and tell me how you get on.

Donald Hughes
Donald Hughes United States
10/28/2008 5:33:02 PM #

It looks really cool.  I was suprised that it let me get thur the install.  It really wants to know about the blogger installed.  Infact I attempted installing live writer before and gave up cause I thought it would never get past me not having a microsoft "live" blogger.  But now that you mentioned it I have tried again.  Thanks!

Now that I have it working I wonder how much else this thing will do for me?
I think a quick blog on how you use this thing might be in order.  I already know what BlogEngine.Net does for us.

Again thanks mate!
  

Donald Hughes
Donald Hughes United States
10/29/2008 7:48:31 AM #

Well I just published my first page using windows live writter. WOW! perfect! I'll never use the BlogEngine's WYSIWYG again. It worked behind a firewall and a proxy server!

Hipolito M. Arde
Hipolito M. Arde United States
3/25/2011 2:17:48 PM #

I'll wait for your next post Smile

watch scream 4 online free
watch scream 4 online free United States
4/1/2011 8:13:07 AM #

Thanks guys for taking time to write this post, it was a good read.

Meet sex Partner
Meet sex Partner United States
4/13/2011 3:14:08 AM #

A really inspiring topic - Thank you very much I wish you would'nt mind me blogging about this post on my site I will also leave a linkback Thank you

William Villanueva
William Villanueva United States
4/29/2011 2:56:07 PM #

Thank you very much for the post!

Pingbacks and trackbacks (1)+

Comments are closed