Reputation: 8614
I'm working on a legacy .NET program. The code is very old, like 15-20 years old, and no unit tests were written at the time the program was originally created.
The company has also implemented a number of technologies for scanning source code for vulnerabilities and implemented policies for including unit tests in new code. So I have to implement new unit tests whenever I change anything.
I've implemented some new tests for some new code, and I've added an app.config file to the unit test project to contain database connection strings. The issue is that the unit test project's app.config file is failing the vulnerability scan because of an unencrypted password in a connection string.
To fix this, I want to add a section in the app.config file that will be encrypted and contain the password, and add a connection string. Here's the full unencrypted app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, culture=neutral, PublicKeyToken="b77a5c561934e089" />
<section name="encryptedAppSettings" type=System.Configuration.NameValueSectionHandler, Culture-neutral />
</configSections>
<ConfigProtectedData>
<providers>
<add name="MyProvider" type="System.Configuration.DpapiProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=newutral,PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</ConfigProtectedData>
<connectionStrings>
<add name="UnitTests.Properties.Settings.connectionString" connectionString="...UserId=username,Password={0}" />
</connectionStrings>
<encryptedAppSettings>
<add name="UnitTests.Properties.Settings.connectionString.password" value="password" />
</encryptedAppSettings>
</configuration>
My problem right now is that I try to retrieve the section using ConfigurationManager.GetSection("encryptedAppSetting") and it returns null. What am I doing wrong?
EDIT:
I'm getting the following exception when I execute ConfigurationManager.GetSection("encryptedAppSettings"):
System.Configuration.ConfigurationException: Required attribute 'Key' not found.(C:\Users\myName\source\repos\myproject...")
Upvotes: 0
Views: 36
Reputation: 4153
NameValueSectionHandler
uses key/value
pair to define children of the node (see example code).
So the correct section definition in this case should be:
<encryptedAppSettings>
<add key="UnitTests.Properties.Settings.connectionString.password" value="password" />
</encryptedAppSettings>
Upvotes: 0