Click here to Skip to main content
15,884,176 members
Articles / Web Development / ASP.NET

Basic Example of On-The-Fly Config Section Encryption

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
14 May 2010CPOL 12.9K   3   7
Basic Example of On-The-Fly Config Section Encryption

I recently answered a question about how you could securely modify a value within an encrypted configuration website without having to create a duplicate config file. Well, the answer’s pretty simple and straightforward – just use the API!

A quick example page could be:

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
     <asp:Button ID="EncryptButton" runat="server" Text="Encrypt" 
	OnClick="EncryptButton_Click" />
     <asp:Button ID="DecryptButton" runat="server" Text="Decrypt" 
	OnClick="DecryptButton_Click" />
     <asp:Button ID="IncrementButton" runat="server" Text="Increment" 
	OnClick="IncrementButton_Click" />
     <asp:Label ID="CountLabel" runat="server" Text="0"></asp:Label>
     <br />
     <asp:Label ID="StatusLabel" runat="server" Text="" 
	EnableViewState="false"></asp:Label>
 </div>
 </form>
</body>
</html>
C#
using System;
using System.Configuration;
using System.Linq;
using System.Web.Configuration;

public partial class _Default : System.Web.UI.Page
{
 /// <summary>
 /// Handles the PreRender event of the Page control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="System.EventArgs"/> 
 /// instance containing the event data.</param>
 protected void Page_PreRender(object sender, EventArgs e)
 {      
     Configuration confg = WebConfigurationManager.OpenWebConfiguration
	(Request.ApplicationPath);
     ConfigurationSection confStrSect = confg.GetSection(sectionKey);
     StatusLabel.Text = "No Config Section";
     if (confStrSect != null)
     {
         StatusLabel.Text = "Decrypted";
         if (confStrSect.SectionInformation.IsProtected)
         {
             StatusLabel.Text = "Encrypted";
         }
         int count = 0;
         int.TryParse(confg.AppSettings.Settings[countKey].Value, out count);
         CountLabel.Text = count.ToString();
     }   
 }
 
 string providerKey = "RSAProtectedConfigurationProvider";
 string sectionKey = "appSettings";
 string countKey = "Count";

 /// <summary>
 /// Handles the Click event of the EncryptButton control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="System.EventArgs"/> 
 /// instance containing the event data.</param>
 protected void EncryptButton_Click(object sender, EventArgs e)
 {
     Configuration confg = WebConfigurationManager.OpenWebConfiguration
	(Request.ApplicationPath);
     ConfigurationSection confStrSect = confg.GetSection(sectionKey);
     if (confStrSect != null)
     {
         confStrSect.SectionInformation.ProtectSection(providerKey);
         confg.Save();
     }
 }
 /// <summary>
 /// Handles the Click event of the DecryptButton control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="System.EventArgs"/> 
 /// instance containing the event data.</param>
 protected void DecryptButton_Click(object sender, EventArgs e)
 {
     Configuration confg = WebConfigurationManager.OpenWebConfiguration
	(Request.ApplicationPath);
     ConfigurationSection confStrSect = confg.GetSection(sectionKey);
     if (confStrSect != null && confStrSect.SectionInformation.IsProtected)
     {
         confStrSect.SectionInformation.UnprotectSection();
         confg.Save();
     }
 }
 /// <summary>
 /// Handles the Click event of the IncrementButton control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="System.EventArgs"/> 
 /// instance containing the event data.</param>
 protected void IncrementButton_Click(object sender, EventArgs e)
 {
     int count = 0;
     int.TryParse(WebConfigurationManager.AppSettings[countKey], out count);
     count++;
     
     Configuration confg = WebConfigurationManager.OpenWebConfiguration
	(Request.ApplicationPath);
     ConfigurationSection confStrSect = confg.GetSection(sectionKey);
     {
         if (confg.AppSettings.Settings.AllKeys.Contains(countKey))
         {
             confg.AppSettings.Settings[countKey].Value = count.ToString();
         }
         else
         {
             confg.AppSettings.Settings.Add(countKey, count.ToString());
         }
         confg.Save(ConfigurationSaveMode.Modified);
     }
 }
}

Simple!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Freestyle Interactive Ltd
United Kingdom United Kingdom
I'm a lead developer for Freestyle Interactive Ltd where we create many wonderful websites built on Microsofts ASP.Net and Ektron CMS.

I've been developing .Net applications (both Windows and Web) since 2002.

Comments and Discussions

 
Generallet IIS do the work Pin
Bob Crowley18-May-10 10:29
Bob Crowley18-May-10 10:29 
GeneralRe: let IIS do the work Pin
Martin Jarvis18-May-10 10:51
Martin Jarvis18-May-10 10:51 
GeneralRe: let IIS do the work Pin
Bob Crowley19-May-10 2:10
Bob Crowley19-May-10 2:10 
GeneralRe: let IIS do the work Pin
Martin Jarvis19-May-10 2:41
Martin Jarvis19-May-10 2:41 
GeneralWep App restart Pin
abhidhar17-May-10 18:54
abhidhar17-May-10 18:54 
GeneralRe: Wep App restart Pin
Martin Jarvis18-May-10 10:53
Martin Jarvis18-May-10 10:53 
GeneralRe: Wep App restart Pin
abhidhar18-May-10 18:58
abhidhar18-May-10 18:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.