-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionStringHelper.cs
executable file
·147 lines (127 loc) · 5.73 KB
/
ConnectionStringHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfigSecurity
{
public static class ConnectionStringHelper
{
public static void ProtectConnectionStrings(string pathName)
{
ToggleProtectConnectionStrings(pathName, true);
}
public static void UnprotectConnectionStrings(string pathName)
{
ToggleProtectConnectionStrings(pathName, false);
}
public static void ProtectApplicationSettings(string pathName)
{
ToggleProtectApplicationSettings(pathName, true);
}
public static void UnprotectApplicationSettings(string pathName)
{
ToggleProtectApplicationSettings(pathName, false);
}
private static void ToggleProtectConnectionStrings(string pathName, bool protect)
{
// Define the Dpapi provider name.
string strProvider = "DataProtectionConfigurationProvider";
// string strProvider = "RSAProtectedConfigurationProvider";
Configuration config = null;
// For Web!
// oConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
// For Windows!
// Takes the executable file name without the config extension.
config = ConfigurationManager.OpenExeConfiguration(pathName);
if (config != null)
{
var configSection = config.GetSection("connectionStrings") as ConnectionStringsSection;
if (configSection != null)
{
var hasChanged = false;
if ((!(configSection.ElementInformation.IsLocked)) && (!(configSection.SectionInformation.IsLocked)))
{
if (protect)
{
// Encrypt the section.
if (!(configSection.SectionInformation.IsProtected))
{
hasChanged = true;
configSection.SectionInformation.ProtectSection(strProvider);
}
}
else
{
// Remove encryption.
if (configSection.SectionInformation.IsProtected)
{
hasChanged = true;
configSection.SectionInformation.UnprotectSection();
}
}
}
if (hasChanged)
{
// Save current configuration.
configSection.SectionInformation.ForceSave = true;
config.Save();
}
}
}
}
private static void ToggleProtectApplicationSettings(string pathName, bool protect)
{
// Define the Dpapi provider name.
string strProvider = "DataProtectionConfigurationProvider";
// string strProvider = "RSAProtectedConfigurationProvider";
Configuration oConfiguration = null;
// For Web!
// oConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
// For Windows!
// Takes the executable file name without the config extension.
oConfiguration = ConfigurationManager.OpenExeConfiguration(pathName);
if (oConfiguration != null)
{
// Retrieve the applicationSettings section.
ConfigurationSectionGroup applicationSettings = oConfiguration.GetSectionGroup("applicationSettings");
if (applicationSettings != null && applicationSettings.Sections != null)
{
ConfigurationSectionCollection configSections = applicationSettings.Sections;
foreach (ConfigurationSection configSection in configSections)
{
var changed = false;
if ((!(configSection.ElementInformation.IsLocked)) && (!(configSection.SectionInformation.IsLocked)))
{
if (protect)
{
// Encrypt the section.
if (!(configSection.SectionInformation.IsProtected))
{
changed = true;
configSection.SectionInformation.ProtectSection(strProvider);
}
}
else
{
// Remove encryption.
if (configSection.SectionInformation.IsProtected)
{
changed = true;
configSection.SectionInformation.UnprotectSection();
}
}
}
if (changed)
{
// Save current configuration.
configSection.SectionInformation.ForceSave = true;
oConfiguration.Save();
}
}
}
}
}
}
}