Saturday, January 8, 2011

Creating Custom Sections and Encrypting the Section in web.config

Creating Custom Sections and Encryption in web.config
Web application development need different kind of customization,one of the common scenario is creation custom sections in web.config.For the ease of development .net framework supports different set of custom sections.There are mainly three kind 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 keep a user credential information.(Below we will talk 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 custom section so you can define /name you 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 use full to keep the data in custom section so you can define /name you section properly in web.config. You could read the data as a hash table.
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 &gt;

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 secured. There are 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 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

No comments:

Post a Comment