andrew4582

WebConfigProtection

Jan 14th, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. #define Net4
  2. #if Net4
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Configuration;
  8. using System.Web.Configuration;
  9. using System.Web;
  10.  
  11. namespace Core.Configuration {
  12.  
  13.     /// <summary>
  14.     /// Protects/UnProtects Web.config file sections
  15.     /// </summary>
  16.     public class WebConfigProtection {
  17.  
  18.         HttpRequestBase Request { get; set; }
  19.  
  20.         public WebConfigProtection(HttpRequestBase req) {
  21.             Request = req;
  22.         }
  23.  
  24.  
  25.         public void ProtectSection(string sectionName,string provider = "DataProtectionConfigurationProvider") {
  26.  
  27.             System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
  28.  
  29.             ConfigurationSection section = config.GetSection(sectionName);
  30.  
  31.             if(section != null && !section.SectionInformation.IsProtected) {
  32.                 section.SectionInformation.ProtectSection(provider);
  33.                 config.Save();
  34.             }
  35.         }
  36.  
  37.         public void UnProtectSection(string sectionName) {
  38.  
  39.             System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
  40.  
  41.             ConfigurationSection section = config.GetSection(sectionName);
  42.  
  43.             if(section != null && section.SectionInformation.IsProtected) {
  44.                 section.SectionInformation.UnprotectSection();
  45.                 config.Save();
  46.             }
  47.         }
  48.     }
  49. }
  50. #endif
Advertisement
Add Comment
Please, Sign In to add comment