Encrypt Web.config file - asp.net

This is an example of how to encrypt a 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





Popular posts from this blog

How to create zip file to download in JSP- Servlet

How to create DataGrid or GridView in JSP - Servlet

Pinging in ASP.NET Example