Securing Connection String Data

hop6919

New member
Joined
Sep 28, 2009
Messages
2
Programming Experience
Beginner
Hi All,

I wonder if you can help me understand a piece of code a little more. I'm currently working my way through an MCTS Self Paced Training Kit (70-505, Chapter 5, Lesson 6). This particular lesson teaches you about securing connection string data. It explains the basic premise, and asks you to 'copy and paste' the following code into Visual Studios to demonstrate it. I do like to understand why a particular piece of code works before I move on, but for the life of me I can't quite see it.

Here is the code: (a reference is also needed to System.Configuration in the properties)

VB.NET:
Imports System
Imports System.Configuration

Public Class Form1

Private Sub EncryptConnectionString()

' Get the configuration file
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

' Create the provider name
Dim provider As String = "DataProtectionConfigurationProvider"

' Encrypt the ConnectionStrings
Dim connStrings As ConfigurationSection = config.ConnectionStrings
connStrings.SectionInformation.ProtectSection(provider)
connStrings.SectionInformation.ForceSave = True
config.Save(ConfigurationSaveMode.Full)
End Sub


I understand that this code:
a] creates the configuration file
b] puts the connection string in the config file
c] encrypts the section with connection string in it

What I'd like to know is how the code actually infers this! I can see that:
connStrings.SectionInformation.ProtectSection(provider) encrypts the section named DataProtectionConfigurationProvider.

But I can't understand how the code that 'gets the configuration file' works, and how the application would know how to search that file for it, as opposed to within the compiled data - there seems to be no method called that instructs the application to do that!

As they say.... ask a stupid question and you'll feel a fool for five minutes....

Thanks very much in advance!
 
As far as I see it, it loads the Projects-Configuration file. You can have a look at that file under My Project -> Settings, it's a generated XML-File which will be deployed with your application (if needed), and were you can store variables which have to survive a restart of the application (pretty similar to an ini file). With OpenExeConfiguration the app is looking for that configuration file in the same directory as the exe, if the file is missing, it should create a new one or (which is most likely) throw an exception.

Bobby
 
Thanks - that's a little clearer. After some more research, I realised that the code I didn't understand was the method!
 
Back
Top