jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
namespace HW.Utility
{
    using System;
    using System.Configuration;
 
    public static class ProtectAppConfig
    {
        public static T OpenConfigFile<T>(string pathName, string stringSection) where T: ConfigurationSection
        {
            System.Configuration.Configuration configuration = null;
            T section = default(T);
            try
            {
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = pathName;
                configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                if (configuration != null)
                {
                    section = configuration.GetSection(stringSection) as T;
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return section;
        }
 
        public static void ProtectConfigFiles(string pathName, string protectProvider, string protectSection)
        {
            if (protectSection == "All")
            {
                ToggleConnectionStringProtection(pathName, true, protectProvider);
                ToggleAppSettingProtection(pathName, true, protectProvider);
            }
            else if (protectSection == "connectionStrings")
            {
                ToggleConnectionStringProtection(pathName, true, protectProvider);
            }
            else
            {
                ToggleAppSettingProtection(pathName, true, protectProvider);
            }
        }
 
        private static void ToggleAppSettingProtection(string pathName, bool protect, string protectProvider)
        {
            System.Configuration.Configuration configuration = null;
            AppSettingsSection section = null;
            bool flag = false;
            try
            {
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = pathName;
                configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                if (configuration != null)
                {
                    section = configuration.GetSection("appSettings") as AppSettingsSection;
                    if (section != null)
                    {
                        if (!section.ElementInformation.IsLocked && !section.SectionInformation.IsLocked)
                        {
                            if (protect)
                            {
                                if (!section.SectionInformation.IsProtected)
                                {
                                    flag = true;
                                    section.SectionInformation.ProtectSection(protectProvider);
                                }
                            }
                            else if (section.SectionInformation.IsProtected)
                            {
                                flag = true;
                                section.SectionInformation.UnprotectSection();
                            }
                        }
                        if (flag)
                        {
                            section.SectionInformation.ForceSave = true;
                            configuration.Save(ConfigurationSaveMode.Modified);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
 
        private static void ToggleConnectionStringProtection(string pathName, bool protect, string protectProvider)
        {
            System.Configuration.Configuration configuration = null;
            ConnectionStringsSection section = null;
            bool flag = false;
            try
            {
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = pathName;
                configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                if (configuration != null)
                {
                    section = configuration.GetSection("connectionStrings") as ConnectionStringsSection;
                    if (section != null)
                    {
                        if (!section.ElementInformation.IsLocked && !section.SectionInformation.IsLocked)
                        {
                            if (protect)
                            {
                                if (!section.SectionInformation.IsProtected)
                                {
                                    flag = true;
                                    section.SectionInformation.ProtectSection(protectProvider);
                                }
                            }
                            else if (section.SectionInformation.IsProtected)
                            {
                                flag = true;
                                section.SectionInformation.UnprotectSection();
                            }
                        }
                        if (flag)
                        {
                            section.SectionInformation.ForceSave = true;
                            configuration.Save(ConfigurationSaveMode.Modified);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
 
        public static void UnProtectConfigFiles(string pathName, string protectProvider, string protectSection)
        {
            if (protectSection == "All")
            {
                ToggleConnectionStringProtection(pathName, false, protectProvider);
                ToggleAppSettingProtection(pathName, false, protectProvider);
            }
            else if (protectSection == "connectionStrings")
            {
                ToggleConnectionStringProtection(pathName, false, protectProvider);
            }
            else
            {
                ToggleAppSettingProtection(pathName, false, protectProvider);
            }
        }
 
        public static bool UpdateConnectionString(string pathName, string stringSection, string strContent)
        {
            System.Configuration.Configuration configuration = null;
            ConnectionStringsSection section = null;
            AppSettingsSection section2 = null;
            bool flag = false;
            try
            {
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = pathName;
                if (fileMap != null)
                {
                    configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                }
                if (configuration == null)
                {
                    return flag;
                }
                if (stringSection == "connectionStrings")
                {
                    section = configuration.GetSection(stringSection) as ConnectionStringsSection;
                    if ((section != null) && !(section.ElementInformation.IsLocked || section.SectionInformation.IsLocked))
                    {
                        section.ConnectionStrings["FBMSConnection"].ConnectionString = strContent;
                        section.SectionInformation.ForceSave = true;
                        configuration.Save(ConfigurationSaveMode.Modified);
                        flag = true;
                    }
                    return flag;
                }
                if (!(stringSection == "appSettings"))
                {
                    return flag;
                }
                section2 = configuration.GetSection(stringSection) as AppSettingsSection;
                if (section2 == null)
                {
                    return flag;
                }
                if (!(section2.ElementInformation.IsLocked || section2.SectionInformation.IsLocked))
                {
                    section2.Settings["VerifyURL"].Value = strContent;
                    section2.SectionInformation.ForceSave = true;
                    configuration.Save(ConfigurationSaveMode.Modified);
                    flag = true;
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return flag;
        }
    }
}