Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encrypting applicationSettings nodes in app.config

Status
Not open for further replies.

PPettit

IS-IT--Management
Sep 13, 2003
511
US
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:
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top