Encrypt Connection Strings in VS 2005 .config Files
In present editions of .NET, you can build connections with a .udl file, and copy and paste into a .config file. If you cleverly decided to encrypt your connection string, you could use the DataProtection (DPAPI) wrapper created in "Encrypt DataSets for Offline Storage". All of these capabilities still exist, but they have been consolidated and will be easier to use in Visual Studio 2005once you know where the changes were made.
This article shows you how to add a connection string to your app.config file in Visual Studio 2005, encrypt that connection string, and introduce a tool for automatically encrypting connection strings for ASP.NET.
Adding a Connection String to Project Settings
With so much technology changing all the time, it is easy to have a Homer Simpson moment—D'oh!—and later realize you did something the old way when a new easier way exists. I do this all the time and am sure I am not the only one. (I hate when that happens.) Project settings and .config files will provide ample opportunity for Homer moments in the near future.
In Visual Studio 2005, Microsoft is increasing a reliance on XML but seems to be moving away from the requirement that you have to write XML directly to use it. XML makes a better storage medium than an author medium—that is, it is great to use but unnatural to write—and in Visual Studio 2005 the XML in an App.config file can be managed by Project Properties pages. For example, to add elements that were traditionally application settings we can select Project|<projectname> Properties, change to the Settings tab and click to begin adding settings using a visual designer instead of writing XML.
For example, to add a connection string, follow these steps:
- Ensuring the Settings tab of the project properties page is open, add a new item named ConnectionString.
- Change the type to (Connection String) and the scope to Application.
- In the Value column, click the elided button to open the connection string designer (see Figure 1).
- Define and test the connection string as you would using the Data Link Properties editor defined for .udl files. Click OK.
Figure 1: The integrated connection string builder is used like Data Link Properties editor.
After completing Step 4, the Value field—see Figure 2—will contain the connection string for your server. (The actual server doesn't matter, but I used the Northwind sample database for SQL Server for the example.)
Figure 2: The Settings tab is used to manage elements in configuration files.