SIGN UP MEMBER LOGIN:    
ARTICLE

Creating Custom Sections and Encryption in web.config

Posted by Shyju Articles | WebForms Controls January 09, 2011
Web application development requires various kinds of customization. One of the common scenarios is creation of custom sections in web.config.
Reader Level:


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.
  

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor