Welcome to MSDN Blogs Sign in | Join | Help

BenkoBLOG

Find out what's possible with all this new technology stuff...

Syndication

Encrypting the Web.config in VB

I got a request for how to do the encryption of the web.config but this time in VB, so I thought I'd post it here on the blog as well. The logic is about the same, although I found that in VB I had to add a line to the configuration to save the new settings.  The code for this in vb.net (adding to the global.asax file in the "Session_Start" subroutine:

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)

 

    ' Code that runs when a new session is started

 

    EncryptSection("appSettings")

 

End Sub

 

Private Sub EncryptSection(ByVal sSection As String)

 

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

 

    Dim configSection As ConfigurationSection = config.GetSection(sSection)

 

    If configSection.SectionInformation.IsProtected = False Then

 

        configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")

 

        config.Save()

 

    End If

 

End Sub

 

Published Wednesday, April 16, 2008 6:45 PM by benko

Filed under: , ,

Comments

# Microsoft news and tips » Encrypting the Web.config in VB @ Wednesday, April 16, 2008 2:41 PM

PingBack from http://microsoftnews.askpcdoc.com/?p=2856

Microsoft news and tips » Encrypting the Web.config in VB

# re: Encrypting the Web.config in VB @ Wednesday, April 16, 2008 4:26 PM

I *still* don't understand though why you're doing it in Session_Start rather than Application_Start (see comment to previous entry) - I know it's easier for testing (always a consideration with this sort of thing because of all the fun with file permissions), but in production would you really want to be performing that test every time a new Session starts?

Kevin Daly

# re: Encrypting the Web.config in VB @ Tuesday, May 06, 2008 11:58 AM

You could do it either way. If you were to do the encryption in Application_Start then you need to cycle the web site. Session_start ensures that the data is protected every time a new session runs.

- Mike

benko

Anonymous comments are disabled
Page view tracker