Saturday, 3 October 2015

Encrypt Web config file - asp.net

Leave a Comment
This is example how to encrypt web config file in asp.net.

The example will encrypt web config file for 3 section which is ConnectionString, AppSetting, and system.web/authentication .


Front End aspx Code 

  <br />  
   This is button to encrypt web config.<br />  
 <asp:Button ID="Button1" runat="server" Text="Encrypt Web config" OnClick="Button1_Click" />  

Code Behind

  private static string[] sectionName = { "connectionStrings", "appSettings", "system.web/authentication" };  
     public static string[] SectionName  
     {  
       get  
       {  
         return sectionName;  
       }  
       set  
       {  
         sectionName = value;  
       }  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       //encrypt  
       ProtectSection();  
     }  
     /// <summary>  
     /// Encrypte WebConfig file for certain section  
     /// </summary>  
     /// <param name="sectionName"></param>  
     /// <param name="provider"></param>  
     private void ProtectSection()  
     {  
       string provider = "DataProtectionConfigurationProvider";  
       foreach (string a in SectionName)  
       {  
         Configuration config =  
           WebConfigurationManager.  
             OpenWebConfiguration("~/");  
         ConfigurationSection section =  
                config.GetSection(a);  
         if (section != null &&  
              !section.SectionInformation.IsProtected)  
         {  
           section.SectionInformation.ProtectSection(provider);  
           config.Save();  
         }  
       }  
     }  

Output






By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.