Labels

Thursday, July 31, 2008

03 - Encrypting Sections Programmatically

Hi,

Here we’ll cover the Encrytion of Configuration file.

The topic covered will be –

- Creating Encrypted Configuration Sections

- Encrypting Sections with the aspnet_regiis tool

- Encrypting Sections Programmatically

- Deploying Encrypted Web Configuration Files

Encrypting Sections Programmatically

 
·                Using the Configuration API. 
·                Specifically, you can encrypt a configuration section by calling the 
o                      SectionInformation.ProtectSection() method.
o                      SectionInformation.UnProtectSection()
·                E..g
 

Private Sub Page_Load()

If Not Page.IsPostBack Then

BindSections()

End If

End Sub

Protected Sub grdSections_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

Dim rowIndex As Integer = Int32.Parse(CType(e.CommandArgument, String))

Dim sectionName As String = CType(grdSections.DataKeys(rowIndex).Value, String)

If e.CommandName = "Protect" Then

ProtectSection(sectionName)

End If

If e.CommandName = "UnProtect" Then

UnProtectSection(sectionName)

End If

BindSections()

End Sub

Private Sub BindSections()

Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(Request .ApplicationPath)

Dim colSections As New List(Of SectionInformation)()

For Each section As ConfigurationSection In config.SectionGroups("system.web").Sections

colSections.Add(section.SectionInformation)

Next

grdSections.DataSource = colSections

grdSections.DataBind()

End Sub

Private Sub ProtectSection(ByVal sectionName As String)

Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(Request .ApplicationPath)

Dim section As ConfigurationSection = config.GetSection(sectionName)

section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider")

config.Save(ConfigurationSaveMode.Modified)

End Sub

Private Sub UnProtectSection(ByVal sectionName As String)

Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(Request .ApplicationPath)

Dim section As ConfigurationSection = config.GetSection(sectionName)

section.SectionInformation.UnprotectSection()

config.Save(ConfigurationSaveMode.Modified)

End Sub

 
 
See Figure Link.

Hope this helps

Thanks & Regards,

Arun Manglick || Senior Tech Lead

No comments:

Post a Comment