I've written a Windows Service and need to encrypt the database connection strings as well as a couple of strings added as application settings (username and password for the service). Using some information that I found on CodePlex ( I was able to encrypt the connection strings, but I can't figure out how to encrypt the application settings. It seems like it should just be a matter of figuring out how to address the particular nodes, but nothing I've tried so far has worked. Anyone have any ideas?
Here are the relevant sections from the app.config file:
This is the code that encrypts the entire connection string section:
Here are the relevant sections from the app.config file:
Code:
...
<configuration>
...
<connectionStrings>
<add name="MyServiceName.My.MySettings.ConnectionString"
connectionString="Data Source=MyDBServer;Initial Catalog=MyDataBase;Persist Security Info=True;User ID=mydbusername;Password=mydbuserpassword"
providerName="System.Data.SqlClient" />
</connectionStrings>
...
<applicationSettings>
<MyServiceName.My.MySettings>
...
<setting name="ServiceUserName" serializeAs="String">
<value>mydomain\myusername</value>
</setting>
<setting name="ServicePassword" serializeAs="String">
<value>mypassword</value>
</setting>
</MyServiceName.My.MySettings>
</applicationSettings>
</configuration>
This is the code that encrypts the entire connection string section:
Code:
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.Configuration
Public Class Installer
Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
MyBase.Install(stateSaver)
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(Context.Parameters("assemblypath"))
Dim section As ConfigurationSection = config.GetSection("connectionStrings")
If Not section.SectionInformation.IsProtected Then
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")
config.Save()
End If
End Sub
End Class