Creating Custom Sections and Encryption in web.config



Web application development requires various kinds of customization. One of the common scenarios is creation of custom sections in web.config. For ease of development .Net framework supports multiple sets of custom sections. There are mainly three kinds of custom section.

1. SingleTagSectionHandler

This will help to add a custom single xml tag to web.config. Define your custom
section in <configSections>.

<configSections>
<
section name=" UserInfo"
type="System.Configuration.NameValueSectionHandler"/>
</
configSections>

Add your custom single tag section. For example I would like to keep a user credential information. (Below we will explain how to encrypt.)

<UserInfo  UserName="Myuser"  Password="****" Domain="MyDomain"/>

Read the data from code

(System.Collections.IDictionary) System.Configuration.ConfigurationSettings.GetConfig("UserInfo")

2. NameValueSectionHandler

This will help you to keep key / value pairs of data. It will be usefull to keep the data in a custom section so you can define /name your section properly in web.config.

For example going to create new section for supported extensions and supported special servers.

Define a custom section group

<section name="SupportedExtensions"
type="System.Configuration.NameValueSectionHandler"/>

<section name="SupportedServer"
type="System.Configuration.NameValueSectionHandler"/>

< SupportedExtensions >
    <
add key="Image" value=".jpg,png"/>
    <
add key="Video" value="flv,wmv"/>
</
SupportedExtensions >

< SupportedServer >
    <
add key="AppServer" value="server1,server2"/>
    <
add key="ImageServer" value="server4,server5"/>
</
SupportedServer >


Read the data from code

(System.Collections.NameValueCollection) System.Configuration.ConfigurationSettings.GetConfig("SupportedServer")

3. DictionarySectionHandler

This will help you to keep key / value pairs of data in nested sections. It will be useful to keep the data in a custom section so you can define /name your section properly in web.config.

For example create section for the application.

Define a custom section

<section name="MyApplicatinSection"
type="System.Configuration.DictionarySectionHandler"/>

< MyApplicatinSection >

< SupportedExtensions >
    <
add key="Image" value=".jpg,png"/>
    <
add key="Video" value="flv,wmv"/>
</
SupportedExtensions >

< SupportedServer >
    <
add key="AppServer" value="server1,server2"/>
    <
add key="ImageServer" value="server4,server5"/>
</
SupportedServer >

< MyApplicatinSection >

Read the data from code

(System.Collections.Hashtable) System.Configuration.ConfigurationSettings.GetConfig("MyApplicatinSection")

Encrypting the web.config

Once we define the data in config we need to encrypt the data to keep the information secure. There is an easy way to encrypt the sections.

1. Creat web.config entry

<section name="UserInfoSection"
 
type="System.Configuration.SingleTagSectionHandler"/>

<
UserInfoSection  username="value1" password="value2" domain="value3"/>

2. Encrypt the section

Go to the Visual Studio command prompt and run or go to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

aspnet_regiis –pef  UserInfoSection

3. Read the value from code

MyUserData = (System.Collections.IDictionary)
System.Configuration.ConfigurationSettings.GetConfig("
UserInfoSection ");

Also read http://msdn.microsoft.com/en-us/library/zhhddkxy.aspx.
  


Similar Articles