-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionString.cs
235 lines (199 loc) · 8.48 KB
/
ConnectionString.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity.Core.EntityClient;
using System.Data.SqlClient;
using System.IO;
using System.Web.Configuration;
using System.Xml.Linq;
namespace HCAppCode
{
public class ConnectionSetting
{
public string Provider;
public string ConnectionString;
public ConnectionSetting(string provider)
{
Provider = provider;
}
public ConnectionSetting(string provider, string connectionString, params string[] connectionStringArguments)
{
ConnectionString = string.Format(connectionString, connectionStringArguments); ;
Provider = provider;
}
public bool Test()
{
try
{
using (var conn = new SqlConnection(ConnectionString))
{
conn.Open();
conn.Close();
}
}
catch
{
return false;
}
return true;
}
}
public class SqlConnectionSetting : ConnectionSetting
{
SqlConnectionStringBuilder Builder = new SqlConnectionStringBuilder();
public SqlConnectionSetting(string server, string databaseNameOrPath, string username, string password, bool? multipleActiveResultSets, bool? integratedSecurity, bool? persistSecurityInfo, string applicationName)
: base("System.Data.SqlClient")
{
if (databaseNameOrPath.Contains("\\") || databaseNameOrPath.ToLower().EndsWith(".mdf"))
Builder.AttachDBFilename = databaseNameOrPath;
else
Builder.InitialCatalog = databaseNameOrPath;
Builder.IntegratedSecurity = true;
Builder.UserID = username;
Builder.Password = password;
if (multipleActiveResultSets.HasValue)
Builder.MultipleActiveResultSets = multipleActiveResultSets.Value;
if (integratedSecurity.HasValue)
Builder.IntegratedSecurity = integratedSecurity.Value;
if (persistSecurityInfo.HasValue)
Builder.PersistSecurityInfo = persistSecurityInfo.Value;
if (!string.IsNullOrEmpty(applicationName))
Builder.ApplicationName = applicationName;
var isLocal = string.IsNullOrEmpty(server) || server.Trim().ToLower() == "(local)";
if (isLocal)
{
ConnectionString = "Server=(local);" + Builder.ToString();
}
else
{
Builder.DataSource = server;
ConnectionString = Builder.ToString();
}
//return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=False;User Id={2};Password={3};", server, database, username, password);
}
}
public class EntityConnectionSetting : ConnectionSetting
{
EntityConnectionStringBuilder Builder = new EntityConnectionStringBuilder();
public EntityConnectionSetting(string model, SqlConnectionSetting sqlConnString)
: base("System.Data.EntityClient")
{
Builder.Metadata = string.Format("res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", model);
Builder.Provider = sqlConnString.Provider;
Builder.ProviderConnectionString = sqlConnString.ConnectionString;
ConnectionString = Builder.ToString();
}
public EntityConnectionSetting(string model, string modelProvider, string server, string database, string username, string password, bool? multipleActiveResultSets, bool? integratedSecurity, bool? persistSecurityInfo, string applicationName)
: base("System.Data.EntityClient")
{
Builder.Metadata = string.Format("res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", model);
Builder.Provider = modelProvider;
Builder.ProviderConnectionString = new SqlConnectionSetting(server, database, username, password, multipleActiveResultSets, integratedSecurity, persistSecurityInfo, applicationName).ConnectionString;
ConnectionString = Builder.ToString();
}
public new bool Test()
{
try
{
using (var conn = new EntityConnection(ConnectionString))
{
conn.Open();
conn.Close();
}
}
catch
{
return false;
}
return true;
}
}
public static class WebConfigConnectionStringManager
{
// Modification requires Admin rights may be via impersonation.
public static Configuration Config = WebConfigurationManager.OpenWebConfiguration("~");
public static T GetConfigurationSection<T>(string sectionName) where T : ConfigurationSection
{
try
{
return (T)Config.GetSection(sectionName) as T;
}
catch
{
return null;
}
}
public static void SetConnectionStrings(Dictionary<string, ConnectionSetting> connectionStrings, bool encrypt)
{
var section = GetConfigurationSection<ConnectionStringsSection>("connectionStrings");
if (section == null)
{
var doc = XDocument.Load(Config.FilePath);
doc.Root.Element("connectionStrings").Remove();
doc.Root.Add(new XElement("connectionStrings"));
// Save to temp folder first then overwrite the original web.config file in case it doesn't have write access.
doc.Save(Path.GetTempPath() + "Web.config");
Utils.MoveFile(Path.GetTempPath() + "Web.config", Config.FilePath, false);
section = GetConfigurationSection<ConnectionStringsSection>("connectionStrings");
}
var xmlConnections = new Dictionary<string, string>();
foreach (ConnectionStringSettings con in section.ConnectionStrings)
xmlConnections.Add(con.Name, con.ConnectionString);
var isModified = false;
foreach (var con in connectionStrings)
if (!xmlConnections.ContainsKey(con.Key))
{
section.ConnectionStrings.Add(new ConnectionStringSettings(con.Key, con.Value.ConnectionString.Replace(""", "\""), con.Value.Provider));
isModified = true;
}
//else if (xmlConnections[con.Name] != con.ConnectionString)
//{
// section.ConnectionStrings.Remove(con.Name);
// section.ConnectionStrings.Add(con);
// isModified = true;
//}
isModified = isModified || EncryptConnectionStrings(encrypt);
if (isModified)
Save(true);
}
public static void Save(bool deleteFileIfWriteAccessDenied)
{
var source = Path.GetTempPath() + "Web.config";
var target = Config.FilePath;
// Will only create file if changes exist.
if (deleteFileIfWriteAccessDenied)
{
Config.SaveAs(source, ConfigurationSaveMode.Full, false);
// Returns false if source file does not exist.
Utils.MoveFile(source, target, false);
}
else
{
Config.Save();
}
}
/// <summary>
/// Encrypts connection strings(requires Admin rights may be via impersonation)
/// </summary>
/// <param name="encrypt"></param>
public static bool EncryptConnectionStrings(bool encrypt)
{
var section = GetConfigurationSection<ConnectionStringsSection>("connectionStrings");
// Toggle encryption.
if (encrypt != section.SectionInformation.IsProtected)
{
if (encrypt)
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); // "RSAProtectedConfigurationProvider"
else
section.SectionInformation.UnprotectSection();
WebConfigConnectionStringManager.Save(true);
if (encrypt != section.SectionInformation.IsProtected)
{
throw new Exception("Configuration Encryption Failed");
}
return true;
}
return false;
}
}
}