1 | using System;
|
---|
2 | using System.Collections.Specialized;
|
---|
3 | using System.Configuration;
|
---|
4 | using System.Configuration.Provider;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Security.Cryptography;
|
---|
7 | using System.Text;
|
---|
8 | using System.Web.Configuration;
|
---|
9 | using System.Web.Security;
|
---|
10 | using Persistence;
|
---|
11 |
|
---|
12 | namespace Service.Provider {
|
---|
13 | class HeuristicLabMembershipProvider : MembershipProvider {
|
---|
14 |
|
---|
15 | private string pApplicationName;
|
---|
16 | private bool pEnablePasswordReset;
|
---|
17 | private bool pEnablePasswordRetrieval;
|
---|
18 | private bool pRequiresQuestionAndAnswer;
|
---|
19 | private bool pRequiresUniqueEmail;
|
---|
20 | private int pMaxInvalidPasswordAttempts;
|
---|
21 | private int pPasswordAttemptWindow;
|
---|
22 | private int pMinRequiredPasswordLength;
|
---|
23 | private MembershipPasswordFormat pPasswordFormat = MembershipPasswordFormat.Clear;
|
---|
24 | private MachineKeySection machineKey;
|
---|
25 |
|
---|
26 |
|
---|
27 | public override void Initialize(string name, NameValueCollection config) {
|
---|
28 | //
|
---|
29 | // Initialize values from web.config.
|
---|
30 | //
|
---|
31 |
|
---|
32 | if (config == null)
|
---|
33 | throw new ArgumentNullException("config");
|
---|
34 |
|
---|
35 | if (string.IsNullOrEmpty(name) || name.Length == 0)
|
---|
36 | name = "HeuristicLabMembershipProvider";
|
---|
37 |
|
---|
38 | if (String.IsNullOrEmpty(config["description"])) {
|
---|
39 | config.Remove("description");
|
---|
40 | config.Add("description", "Heuristic Lab Membership provider");
|
---|
41 | }
|
---|
42 |
|
---|
43 | // Initialize the abstract base class.
|
---|
44 | base.Initialize(name, config);
|
---|
45 |
|
---|
46 | pApplicationName = GetConfigValue(config["applicationName"], System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
|
---|
47 | pMaxInvalidPasswordAttempts = Convert.ToInt32(GetConfigValue(config["maxInvalidPasswordAttempts"], "5"));
|
---|
48 | pPasswordAttemptWindow = Convert.ToInt32(GetConfigValue(config["passwordAttemptWindow"], "10"));
|
---|
49 | pMinRequiredPasswordLength = Convert.ToInt32(GetConfigValue(config["minRequiredPasswordLength"], "7"));
|
---|
50 | pEnablePasswordReset = Convert.ToBoolean(GetConfigValue(config["enablePasswordReset"], "true"));
|
---|
51 | pEnablePasswordRetrieval = Convert.ToBoolean(GetConfigValue(config["enablePasswordRetrieval"], "true"));
|
---|
52 | pRequiresQuestionAndAnswer = Convert.ToBoolean(GetConfigValue(config["requiresQuestionAndAnswer"], "false"));
|
---|
53 | pRequiresUniqueEmail = Convert.ToBoolean(GetConfigValue(config["requiresUniqueEmail"], "true"));
|
---|
54 |
|
---|
55 | string tempFormat = config["passwordFormat"];
|
---|
56 | if (tempFormat == null) {
|
---|
57 | tempFormat = "Clear";
|
---|
58 | }
|
---|
59 |
|
---|
60 | switch (tempFormat) {
|
---|
61 | case "Hashed":
|
---|
62 | pPasswordFormat = MembershipPasswordFormat.Hashed;
|
---|
63 | break;
|
---|
64 | case "Encrypted":
|
---|
65 | pPasswordFormat = MembershipPasswordFormat.Encrypted;
|
---|
66 | break;
|
---|
67 | case "Clear":
|
---|
68 | pPasswordFormat = MembershipPasswordFormat.Clear;
|
---|
69 | break;
|
---|
70 | default:
|
---|
71 | throw new ProviderException("Password format not supported.");
|
---|
72 | }
|
---|
73 |
|
---|
74 | // Get encryption and decryption key information from the configuration.
|
---|
75 | Configuration cfg =
|
---|
76 | WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
|
---|
77 | machineKey = (MachineKeySection)cfg.GetSection("system.web/machineKey");
|
---|
78 |
|
---|
79 | if (machineKey.ValidationKey.Contains("AutoGenerate"))
|
---|
80 | if (PasswordFormat != MembershipPasswordFormat.Clear)
|
---|
81 | throw new ProviderException("Hashed or Encrypted passwords " +
|
---|
82 | "are not supported with auto-generated keys.");
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override string ApplicationName {
|
---|
86 | get { return pApplicationName; }
|
---|
87 | set { pApplicationName = value; }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override bool ChangePassword(string username, string oldPassword, string newPassword) {
|
---|
91 | using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
|
---|
92 | // check database connection
|
---|
93 | if (db == null) {
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 | try {
|
---|
97 | // try to get user
|
---|
98 | HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
|
---|
99 | if (u.ChangePassword(oldPassword, newPassword)) {
|
---|
100 | // save user to database only if needed
|
---|
101 | db.SubmitChanges();
|
---|
102 | return true;
|
---|
103 | } else {
|
---|
104 | return false;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | catch (Exception) {
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) {
|
---|
114 | using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
|
---|
115 | // check database connection
|
---|
116 | if (db == null) {
|
---|
117 | return false;
|
---|
118 | }
|
---|
119 | try {
|
---|
120 | // try to get user
|
---|
121 | HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
|
---|
122 | if (u.ChangePasswordQuestionAndAnswer(password, newPasswordQuestion, newPasswordAnswer)) {
|
---|
123 | // save user to database only if needed
|
---|
124 | db.SubmitChanges();
|
---|
125 | return true;
|
---|
126 | } else {
|
---|
127 | return false;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | catch (Exception) {
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) {
|
---|
137 | using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
|
---|
138 | // check database connection
|
---|
139 | if (db == null) {
|
---|
140 | status = MembershipCreateStatus.ProviderError;
|
---|
141 | return null;
|
---|
142 | }
|
---|
143 | try {
|
---|
144 | // check for duplicate entries
|
---|
145 | if (db.HeuristicLabUsers.Count(x => x.UserName == username) > 0) {
|
---|
146 | status = MembershipCreateStatus.DuplicateUserName;
|
---|
147 | return null;
|
---|
148 | }
|
---|
149 | if (db.HeuristicLabUsers.Count(x => x.Email == email) > 0) {
|
---|
150 | status = MembershipCreateStatus.DuplicateEmail;
|
---|
151 | return null;
|
---|
152 | }
|
---|
153 |
|
---|
154 | // create new user
|
---|
155 | HeuristicLabUser u = new HeuristicLabUser(username, email, passwordQuestion, "");
|
---|
156 | u.ChangePassword("INIT", password);
|
---|
157 | u.ChangePasswordQuestionAndAnswer(password, passwordQuestion, passwordAnswer);
|
---|
158 | // save user into database
|
---|
159 | db.HeuristicLabUsers.InsertOnSubmit(u);
|
---|
160 | db.SubmitChanges();
|
---|
161 |
|
---|
162 | // success
|
---|
163 | status = MembershipCreateStatus.Success;
|
---|
164 | return u;
|
---|
165 | }
|
---|
166 | catch (Exception) {
|
---|
167 | // error
|
---|
168 | status = MembershipCreateStatus.ProviderError;
|
---|
169 | return null;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | public override bool DeleteUser(string username, bool deleteAllRelatedData) {
|
---|
175 | using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
|
---|
176 | // check database connection
|
---|
177 | if (db == null) {
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 | try {
|
---|
181 | // try to get user
|
---|
182 | HeuristicLabUser u =
|
---|
183 | db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == username);
|
---|
184 |
|
---|
185 | // optionally delete related data
|
---|
186 | if (deleteAllRelatedData) {
|
---|
187 | db.HeuristicLabUserRole.DeleteAllOnSubmit<HeuristicLabUserRole>(u.HeuristicLabUserRoles);
|
---|
188 | }
|
---|
189 |
|
---|
190 | // delete user
|
---|
191 | db.HeuristicLabUsers.DeleteOnSubmit(u);
|
---|
192 | db.SubmitChanges();
|
---|
193 | return true;
|
---|
194 | }
|
---|
195 | catch (Exception) {
|
---|
196 | return false;
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | public override bool EnablePasswordReset {
|
---|
202 | get { throw new NotImplementedException(); }
|
---|
203 | }
|
---|
204 |
|
---|
205 | public override bool EnablePasswordRetrieval {
|
---|
206 | get { throw new NotImplementedException(); }
|
---|
207 | }
|
---|
208 |
|
---|
209 | public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords) {
|
---|
210 | throw new NotImplementedException();
|
---|
211 | }
|
---|
212 |
|
---|
213 | public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) {
|
---|
214 | throw new NotImplementedException();
|
---|
215 | }
|
---|
216 |
|
---|
217 | public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) {
|
---|
218 | throw new NotImplementedException();
|
---|
219 | }
|
---|
220 |
|
---|
221 | public override int GetNumberOfUsersOnline() {
|
---|
222 | throw new NotImplementedException();
|
---|
223 | }
|
---|
224 |
|
---|
225 | public override string GetPassword(string username, string answer) {
|
---|
226 |
|
---|
227 | throw new NotImplementedException();
|
---|
228 | }
|
---|
229 |
|
---|
230 | public override MembershipUser GetUser(string username, bool userIsOnline) {
|
---|
231 | throw new NotImplementedException();
|
---|
232 | }
|
---|
233 |
|
---|
234 | public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) {
|
---|
235 | throw new NotImplementedException();
|
---|
236 | }
|
---|
237 |
|
---|
238 | public override string GetUserNameByEmail(string email) {
|
---|
239 | throw new NotImplementedException();
|
---|
240 | }
|
---|
241 |
|
---|
242 | public override int MaxInvalidPasswordAttempts {
|
---|
243 | get { throw new NotImplementedException(); }
|
---|
244 | }
|
---|
245 |
|
---|
246 | public override int MinRequiredNonAlphanumericCharacters {
|
---|
247 | get { throw new NotImplementedException(); }
|
---|
248 | }
|
---|
249 |
|
---|
250 | public override int MinRequiredPasswordLength {
|
---|
251 | get { throw new NotImplementedException(); }
|
---|
252 | }
|
---|
253 |
|
---|
254 | public override int PasswordAttemptWindow {
|
---|
255 | get { throw new NotImplementedException(); }
|
---|
256 | }
|
---|
257 |
|
---|
258 | public override MembershipPasswordFormat PasswordFormat {
|
---|
259 | get { return pPasswordFormat; }
|
---|
260 | }
|
---|
261 |
|
---|
262 | public override string PasswordStrengthRegularExpression {
|
---|
263 | get { throw new NotImplementedException(); }
|
---|
264 | }
|
---|
265 |
|
---|
266 | public override bool RequiresQuestionAndAnswer {
|
---|
267 | get { throw new NotImplementedException(); }
|
---|
268 | }
|
---|
269 |
|
---|
270 | public override bool RequiresUniqueEmail {
|
---|
271 | get { throw new NotImplementedException(); }
|
---|
272 | }
|
---|
273 |
|
---|
274 | public override string ResetPassword(string username, string answer) {
|
---|
275 | throw new NotImplementedException();
|
---|
276 | }
|
---|
277 |
|
---|
278 | public override bool UnlockUser(string userName) {
|
---|
279 | throw new NotImplementedException();
|
---|
280 | }
|
---|
281 |
|
---|
282 | public override void UpdateUser(MembershipUser user) {
|
---|
283 | throw new NotImplementedException();
|
---|
284 | }
|
---|
285 | /// <summary>
|
---|
286 | /// Validates a user
|
---|
287 | /// </summary>
|
---|
288 | /// <param name="username"></param>
|
---|
289 | /// <param name="password"></param>
|
---|
290 | /// <returns></returns>
|
---|
291 | public override bool ValidateUser(string username, string password) {
|
---|
292 | bool isValid = false;
|
---|
293 | using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
|
---|
294 | if (db == null) {
|
---|
295 | return false;
|
---|
296 | }
|
---|
297 | HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
|
---|
298 | isValid = CheckPassword(password, u.GetPassword());
|
---|
299 | }
|
---|
300 | return isValid;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /// <summary>
|
---|
304 | /// compaiers to passwords
|
---|
305 | /// </summary>
|
---|
306 | /// <param name="password"></param>
|
---|
307 | /// <param name="dbpassword"></param>
|
---|
308 | /// <returns></returns>
|
---|
309 | private bool CheckPassword(string password, string dbpassword) {
|
---|
310 | string pass1 = password;
|
---|
311 | string pass2 = dbpassword;
|
---|
312 |
|
---|
313 | switch (PasswordFormat) {
|
---|
314 | case MembershipPasswordFormat.Encrypted:
|
---|
315 | pass2 = DecodePassword(dbpassword);
|
---|
316 | break;
|
---|
317 | case MembershipPasswordFormat.Hashed:
|
---|
318 | pass1 = EncodePassword(password);
|
---|
319 | break;
|
---|
320 | default:
|
---|
321 | break;
|
---|
322 | }
|
---|
323 |
|
---|
324 | if (pass1 == pass2) {
|
---|
325 | return true;
|
---|
326 | }
|
---|
327 |
|
---|
328 | return false;
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | /// <summary>
|
---|
333 | /// Encodes a password
|
---|
334 | /// </summary>
|
---|
335 | /// <param name="password"></param>
|
---|
336 | /// <returns></returns>
|
---|
337 | private string EncodePassword(string password) {
|
---|
338 | string encodedPassword = password;
|
---|
339 |
|
---|
340 | switch (PasswordFormat) {
|
---|
341 | case MembershipPasswordFormat.Clear:
|
---|
342 | break;
|
---|
343 | case MembershipPasswordFormat.Encrypted:
|
---|
344 | encodedPassword =
|
---|
345 | Convert.ToBase64String(EncryptPassword(Encoding.Unicode.GetBytes(password)));
|
---|
346 | break;
|
---|
347 | case MembershipPasswordFormat.Hashed:
|
---|
348 | HMACSHA1 hash = new HMACSHA1();
|
---|
349 | hash.Key = HexToByte(machineKey.ValidationKey);
|
---|
350 | encodedPassword =
|
---|
351 | Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
|
---|
352 | break;
|
---|
353 | default:
|
---|
354 | throw new ProviderException("Unsupported password format.");
|
---|
355 | }
|
---|
356 |
|
---|
357 | return encodedPassword;
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | /// <summary>
|
---|
362 | /// Decodes a encoded Password
|
---|
363 | /// </summary>
|
---|
364 | /// <param name="encodedPassword"></param>
|
---|
365 | /// <returns></returns>
|
---|
366 | private string DecodePassword(string encodedPassword) {
|
---|
367 | string password = encodedPassword;
|
---|
368 |
|
---|
369 | switch (PasswordFormat) {
|
---|
370 | case MembershipPasswordFormat.Clear:
|
---|
371 | break;
|
---|
372 | case MembershipPasswordFormat.Encrypted:
|
---|
373 | password =
|
---|
374 | Encoding.Unicode.GetString(DecryptPassword(Convert.FromBase64String(password)));
|
---|
375 | break;
|
---|
376 | case MembershipPasswordFormat.Hashed:
|
---|
377 | throw new ProviderException("Cannot unencode a hashed password.");
|
---|
378 | default:
|
---|
379 | throw new ProviderException("Unsupported password format.");
|
---|
380 | }
|
---|
381 |
|
---|
382 | return password;
|
---|
383 | }
|
---|
384 |
|
---|
385 | /// <summary>
|
---|
386 | /// returns byte array of an HexString
|
---|
387 | /// </summary>
|
---|
388 | /// <param name="hexString"></param>
|
---|
389 | /// <returns></returns>
|
---|
390 | private static byte[] HexToByte(string hexString) {
|
---|
391 | byte[] returnBytes = new byte[hexString.Length / 2];
|
---|
392 | for (int i = 0; i < returnBytes.Length; i++)
|
---|
393 | returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
|
---|
394 | return returnBytes;
|
---|
395 | }
|
---|
396 |
|
---|
397 | /// <summary>
|
---|
398 | /// returns the configuration string, if the value is null or empty the default value is returned
|
---|
399 | /// </summary>
|
---|
400 | /// <param name="configValue"></param>
|
---|
401 | /// <param name="defaultValue"></param>
|
---|
402 | /// <returns></returns>
|
---|
403 | private string GetConfigValue(string configValue, string defaultValue) {
|
---|
404 | if (String.IsNullOrEmpty(configValue))
|
---|
405 | return defaultValue;
|
---|
406 |
|
---|
407 | return configValue;
|
---|
408 | }
|
---|
409 | }
|
---|
410 | }
|
---|