SqlMembershipProvider.cs 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. //
  2. // System.Web.Security.SqlMembershipProvider
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. // Chris Toshok ([email protected])
  8. //
  9. // (C) 2003 Ben Maurer
  10. // Copyright (c) 2005,2006 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Configuration;
  35. using System.Configuration.Provider;
  36. using System.Data;
  37. using System.Data.Common;
  38. using System.Text;
  39. using System.Web.Configuration;
  40. using System.Security.Cryptography;
  41. namespace System.Web.Security {
  42. public class SqlMembershipProvider : MembershipProvider {
  43. const int SALT_BYTES = 16;
  44. DateTime DefaultDateTime = new DateTime (1754,1,1).ToUniversalTime();
  45. bool enablePasswordReset;
  46. bool enablePasswordRetrieval;
  47. int maxInvalidPasswordAttempts;
  48. MembershipPasswordFormat passwordFormat;
  49. bool requiresQuestionAndAnswer;
  50. bool requiresUniqueEmail;
  51. int minRequiredNonAlphanumericCharacters;
  52. int minRequiredPasswordLength;
  53. int passwordAttemptWindow;
  54. string passwordStrengthRegularExpression;
  55. TimeSpan userIsOnlineTimeWindow;
  56. ConnectionStringSettings connectionString;
  57. DbProviderFactory factory;
  58. DbConnection connection;
  59. string applicationName;
  60. byte[] init_vector;
  61. static object lockobj = new object();
  62. void InitConnection ()
  63. {
  64. if (connection == null) {
  65. lock (lockobj) {
  66. if (connection != null)
  67. return;
  68. factory = ProvidersHelper.GetDbProviderFactory (connectionString.ProviderName);
  69. connection = factory.CreateConnection();
  70. connection.ConnectionString = connectionString.ConnectionString;
  71. connection.Open ();
  72. }
  73. }
  74. }
  75. void AddParameter (DbCommand command, string parameterName, string parameterValue)
  76. {
  77. DbParameter dbp = command.CreateParameter ();
  78. dbp.ParameterName = parameterName;
  79. dbp.Value = parameterValue;
  80. dbp.Direction = ParameterDirection.Input;
  81. command.Parameters.Add (dbp);
  82. }
  83. void CheckParam (string pName, string p, int length)
  84. {
  85. if (p == null)
  86. throw new ArgumentNullException (pName);
  87. if (p.Length == 0 || p.Length > length || p.IndexOf (",") != -1)
  88. throw new ArgumentException (String.Format ("invalid format for {0}", pName));
  89. }
  90. string HashAndBase64Encode (string s, byte[] salt)
  91. {
  92. byte[] tmp = Encoding.UTF8.GetBytes (s);
  93. byte[] hashedBytes = new byte[salt.Length + tmp.Length];
  94. Array.Copy (salt, hashedBytes, salt.Length);
  95. Array.Copy (tmp, 0, hashedBytes, salt.Length, tmp.Length);
  96. MembershipSection section = (MembershipSection)WebConfigurationManager.GetSection ("system.web/membership");
  97. string alg_type = section.HashAlgorithmType;
  98. if (alg_type == "")
  99. alg_type = "SHA1";
  100. HashAlgorithm alg = HashAlgorithm.Create (alg_type);
  101. hashedBytes = alg.ComputeHash (hashedBytes);
  102. return Convert.ToBase64String (hashedBytes);
  103. }
  104. string EncryptAndBase64Encode (string s)
  105. {
  106. MachineKeySection section = (MachineKeySection)WebConfigurationManager.GetSection ("system.web/machineKey");
  107. if (section.DecryptionKey.StartsWith ("AutoGenerate"))
  108. throw new Exception ("You must explicitly specify a decryption key in the <machineKey> section when using encrypted passwords.");
  109. string alg_type = section.Decryption;
  110. if (alg_type == "Auto")
  111. alg_type = "AES";
  112. SymmetricAlgorithm alg = null;
  113. if (alg_type == "AES")
  114. alg = Rijndael.Create ();
  115. else if (alg_type == "3DES")
  116. alg = TripleDES.Create ();
  117. else
  118. throw new Exception (String.Format ("Unsupported decryption attribute '{0}' in <machineKey> configuration section", alg_type));
  119. ICryptoTransform encryptor = alg.CreateEncryptor (section.DecryptionKey192Bits, init_vector);
  120. byte[] result = Encoding.UTF8.GetBytes (s);
  121. result = encryptor.TransformFinalBlock (result, 0, result.Length);
  122. return Convert.ToBase64String (result);
  123. }
  124. [MonoTODO]
  125. public override bool ChangePassword (string username, string oldPwd, string newPwd)
  126. {
  127. throw new NotImplementedException ();
  128. }
  129. [MonoTODO]
  130. public override bool ChangePasswordQuestionAndAnswer (string username, string password, string newPwdQuestion, string newPwdAnswer)
  131. {
  132. throw new NotImplementedException ();
  133. }
  134. public override MembershipUser CreateUser (string username,
  135. string password,
  136. string email,
  137. string pwdQuestion,
  138. string pwdAnswer,
  139. bool isApproved,
  140. object providerUserKey,
  141. out MembershipCreateStatus status)
  142. {
  143. if (username != null) username = username.Trim ();
  144. if (password != null) password = password.Trim ();
  145. if (email != null) email = email.Trim ();
  146. if (pwdQuestion != null) pwdQuestion = pwdQuestion.Trim ();
  147. if (pwdAnswer != null) pwdAnswer = pwdAnswer.Trim ();
  148. /* some initial validation */
  149. if (username == null || username.Length == 0 || username.Length > 256 || username.IndexOf (",") != -1) {
  150. status = MembershipCreateStatus.InvalidUserName;
  151. return null;
  152. }
  153. if (password == null || password.Length == 0 || password.Length > 128) {
  154. status = MembershipCreateStatus.InvalidPassword;
  155. return null;
  156. }
  157. if (RequiresUniqueEmail && (email == null || email.Length == 0)) {
  158. status = MembershipCreateStatus.InvalidEmail;
  159. return null;
  160. }
  161. if (RequiresQuestionAndAnswer &&
  162. (pwdQuestion == null ||
  163. pwdQuestion.Length == 0 || pwdQuestion.Length > 256)) {
  164. status = MembershipCreateStatus.InvalidQuestion;
  165. return null;
  166. }
  167. if (RequiresQuestionAndAnswer &&
  168. (pwdAnswer == null ||
  169. pwdAnswer.Length == 0 || pwdAnswer.Length > 128)) {
  170. status = MembershipCreateStatus.InvalidAnswer;
  171. return null;
  172. }
  173. if (providerUserKey != null && ! (providerUserKey is Guid)) {
  174. status = MembershipCreateStatus.InvalidProviderUserKey;
  175. return null;
  176. }
  177. /* encode our password/answer using the
  178. * "passwordFormat" configuration option */
  179. string passwordSalt = "";
  180. RandomNumberGenerator rng = RandomNumberGenerator.Create ();
  181. switch (PasswordFormat) {
  182. case MembershipPasswordFormat.Hashed:
  183. byte[] salt = new byte[16];
  184. rng.GetBytes (salt);
  185. passwordSalt = Convert.ToBase64String (salt);
  186. password = HashAndBase64Encode (password, salt);
  187. if (RequiresQuestionAndAnswer)
  188. pwdAnswer = HashAndBase64Encode (pwdAnswer, salt);
  189. break;
  190. case MembershipPasswordFormat.Encrypted:
  191. password = EncryptAndBase64Encode (password);
  192. break;
  193. case MembershipPasswordFormat.Clear:
  194. default:
  195. break;
  196. }
  197. /* make sure the hashed/encrypted password and
  198. * answer are still under 128 characters. */
  199. if (password.Length > 128) {
  200. status = MembershipCreateStatus.InvalidPassword;
  201. return null;
  202. }
  203. if (RequiresQuestionAndAnswer) {
  204. if (pwdAnswer.Length > 128) {
  205. status = MembershipCreateStatus.InvalidAnswer;
  206. return null;
  207. }
  208. }
  209. InitConnection();
  210. DbTransaction trans = connection.BeginTransaction ();
  211. string commandText;
  212. DbCommand command;
  213. try {
  214. Guid applicationId;
  215. Guid userId;
  216. /* get the application id since it seems that inside transactions we
  217. can't insert using subqueries.. */
  218. commandText = @"
  219. SELECT ApplicationId
  220. FROM dbo.aspnet_Applications
  221. WHERE dbo.aspnet_Applications.LoweredApplicationName = LOWER(@ApplicationName)
  222. ";
  223. command = factory.CreateCommand ();
  224. command.Transaction = trans;
  225. command.CommandText = commandText;
  226. command.Connection = connection;
  227. command.CommandType = CommandType.Text;
  228. AddParameter (command, "ApplicationName", ApplicationName);
  229. DbDataReader reader = command.ExecuteReader ();
  230. reader.Read ();
  231. applicationId = reader.GetGuid (0);
  232. reader.Close ();
  233. /* check for unique username, email and
  234. * provider user key, if applicable */
  235. commandText = @"
  236. SELECT COUNT(*)
  237. FROM dbo.aspnet_Users u, dbo.aspnet_Applications a
  238. WHERE u.LoweredUserName = LOWER(@UserName)
  239. AND u.ApplicationId = a.ApplicationId
  240. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  241. command = factory.CreateCommand ();
  242. command.Transaction = trans;
  243. command.CommandText = commandText;
  244. command.Connection = connection;
  245. command.CommandType = CommandType.Text;
  246. AddParameter (command, "UserName", username);
  247. AddParameter (command, "ApplicationName", ApplicationName);
  248. if (0 != (int)command.ExecuteScalar()) {
  249. status = MembershipCreateStatus.DuplicateUserName;
  250. trans.Rollback ();
  251. return null;
  252. }
  253. if (requiresUniqueEmail) {
  254. commandText = @"
  255. SELECT COUNT(*)
  256. FROM dbo.aspnet_Membership, dbo.aspnet_Applications
  257. WHERE dbo.aspnet_Membership.Email = @Email
  258. AND dbo.aspnet_Membership.ApplicationId = dbo.aspnet_Applications.ApplicationId
  259. AND dbo.aspnet_Applications.LoweredApplicationName = LOWER(@ApplicationName)";
  260. command = factory.CreateCommand ();
  261. command.Transaction = trans;
  262. command.CommandText = commandText;
  263. command.Connection = connection;
  264. command.CommandType = CommandType.Text;
  265. AddParameter (command, "Email", email);
  266. AddParameter (command, "ApplicationName", ApplicationName);
  267. if (0 != (int)command.ExecuteScalar()) {
  268. status = MembershipCreateStatus.DuplicateEmail;
  269. trans.Rollback ();
  270. return null;
  271. }
  272. }
  273. if (providerUserKey != null) {
  274. commandText = @"
  275. SELECT COUNT(*)
  276. FROM dbo.aspnet_Membership, dbo.aspnet_Applications
  277. WHERE dbo.aspnet_Membership.UserId = @ProviderUserKey
  278. AND dbo.aspnet_Membership.ApplicationId = dbo.aspnet_Applications.ApplicationId
  279. AND dbo.aspnet_Applications.LoweredApplicationName = LOWER(@ApplicationName)";
  280. command = factory.CreateCommand ();
  281. command.Transaction = trans;
  282. command.CommandText = commandText;
  283. command.Connection = connection;
  284. command.CommandType = CommandType.Text;
  285. AddParameter (command, "Email", email);
  286. AddParameter (command, "ApplicationName", ApplicationName);
  287. if (0 != (int)command.ExecuteScalar()) {
  288. status = MembershipCreateStatus.DuplicateProviderUserKey;
  289. trans.Rollback ();
  290. return null;
  291. }
  292. }
  293. /* first into the Users table */
  294. commandText = @"
  295. INSERT into dbo.aspnet_Users (ApplicationId, UserId, UserName, LoweredUserName, LastActivityDate)
  296. VALUES (@ApplicationId, NEWID(), @UserName, LOWER(@UserName), GETDATE())
  297. ";
  298. command = factory.CreateCommand ();
  299. command.Transaction = trans;
  300. command.CommandText = commandText;
  301. command.Connection = connection;
  302. command.CommandType = CommandType.Text;
  303. AddParameter (command, "UserName", username);
  304. AddParameter (command, "ApplicationId", applicationId.ToString());
  305. if (command.ExecuteNonQuery() != 1) {
  306. status = MembershipCreateStatus.UserRejected; /* XXX */
  307. trans.Rollback ();
  308. return null;
  309. }
  310. /* then get the newly created userid */
  311. commandText = @"
  312. SELECT UserId
  313. FROM dbo.aspnet_Users
  314. WHERE dbo.aspnet_Users.LoweredUserName = LOWER(@UserName)
  315. ";
  316. command = factory.CreateCommand ();
  317. command.Transaction = trans;
  318. command.CommandText = commandText;
  319. command.Connection = connection;
  320. command.CommandType = CommandType.Text;
  321. AddParameter (command, "UserName", username);
  322. reader = command.ExecuteReader ();
  323. reader.Read ();
  324. userId = reader.GetGuid (0);
  325. reader.Close ();
  326. /* then insert into the Membership table */
  327. commandText = String.Format (@"
  328. INSERT into dbo.aspnet_Membership
  329. VALUES (@ApplicationId,
  330. @UserId,
  331. @Password, @PasswordFormat, @PasswordSalt,
  332. NULL,
  333. {0}, {1},
  334. {2}, {3},
  335. 0, 0,
  336. GETDATE(), GETDATE(), @DefaultDateTime,
  337. @DefaultDateTime,
  338. 0, @DefaultDateTime, 0, @DefaultDateTime, NULL)",
  339. email == null ? "NULL" : "@Email",
  340. email == null ? "NULL" : "LOWER(@Email)",
  341. pwdQuestion == null ? "NULL" : "@PasswordQuestion",
  342. pwdAnswer == null ? "NULL" : "@PasswordAnswer");
  343. command = factory.CreateCommand ();
  344. command.Transaction = trans;
  345. command.CommandText = commandText;
  346. command.Connection = connection;
  347. command.CommandType = CommandType.Text;
  348. AddParameter (command, "ApplicationId", applicationId.ToString());
  349. AddParameter (command, "UserId", userId.ToString());
  350. if (email != null)
  351. AddParameter (command, "Email", email);
  352. AddParameter (command, "Password", password);
  353. AddParameter (command, "PasswordFormat", ((int)PasswordFormat).ToString());
  354. AddParameter (command, "PasswordSalt", passwordSalt);
  355. if (pwdQuestion != null)
  356. AddParameter (command, "PasswordQuestion", pwdQuestion);
  357. if (pwdAnswer != null)
  358. AddParameter (command, "PasswordAnswer", pwdAnswer);
  359. AddParameter (command, "DefaultDateTime", DefaultDateTime.ToString());
  360. if (command.ExecuteNonQuery() != 1) {
  361. status = MembershipCreateStatus.UserRejected; /* XXX */
  362. return null;
  363. }
  364. trans.Commit ();
  365. status = MembershipCreateStatus.Success;
  366. return GetUser (username, false);
  367. }
  368. catch {
  369. status = MembershipCreateStatus.ProviderError;
  370. trans.Rollback ();
  371. return null;
  372. }
  373. }
  374. [MonoTODO]
  375. public override bool DeleteUser (string username, bool deleteAllRelatedData)
  376. {
  377. CheckParam ("username", username, 256);
  378. if (deleteAllRelatedData) {
  379. /* delete everything from the
  380. * following features as well:
  381. *
  382. * Roles
  383. * Profile
  384. * WebParts Personalization
  385. */
  386. }
  387. DbTransaction trans = connection.BeginTransaction ();
  388. DbCommand command;
  389. string commandText;
  390. InitConnection();
  391. try {
  392. /* delete from the Membership table */
  393. commandText = @"
  394. DELETE dbo.aspnet_Membership
  395. FROM dbo.aspnet_Membership, dbo.aspnet_Users, dbo.aspnet_Applications
  396. WHERE dbo.aspnet_Membership.UserId = dbo.aspnet_Users.UserId
  397. AND dbo.aspnet_Membership.ApplicationId = dbo.aspnet_Applications.ApplicationId
  398. AND dbo.aspnet_Users.LoweredUserName = LOWER (@UserName)
  399. AND dbo.aspnet_Users.ApplicationId = dbo.aspnet_Applications.ApplicationId
  400. AND dbo.aspnet_Applications.LoweredApplicationName = LOWER(@ApplicationName)";
  401. command = factory.CreateCommand ();
  402. command.Transaction = trans;
  403. command.CommandText = commandText;
  404. command.Connection = connection;
  405. command.CommandType = CommandType.Text;
  406. AddParameter (command, "UserName", username);
  407. AddParameter (command, "ApplicationName", ApplicationName);
  408. if (1 != command.ExecuteNonQuery())
  409. throw new ProviderException ("failed to delete from Membership table");
  410. /* delete from the User table */
  411. commandText = @"
  412. DELETE dbo.aspnet_Users
  413. FROM dbo.aspnet_Users, dbo.aspnet_Applications
  414. WHERE dbo.aspnet_Users.LoweredUserName = LOWER (@UserName)
  415. AND dbo.aspnet_Users.ApplicationId = dbo.aspnet_Applications.ApplicationId
  416. AND dbo.aspnet_Applications.LoweredApplicationName = LOWER(@ApplicationName)";
  417. command = factory.CreateCommand ();
  418. command.Transaction = trans;
  419. command.CommandText = commandText;
  420. command.Connection = connection;
  421. command.CommandType = CommandType.Text;
  422. AddParameter (command, "UserName", username);
  423. AddParameter (command, "ApplicationName", ApplicationName);
  424. if (1 != command.ExecuteNonQuery())
  425. throw new ProviderException ("failed to delete from User table");
  426. trans.Commit ();
  427. return true;
  428. }
  429. catch {
  430. trans.Rollback ();
  431. return false;
  432. }
  433. }
  434. [MonoTODO]
  435. public virtual string GeneratePassword ()
  436. {
  437. throw new NotImplementedException ();
  438. }
  439. [MonoTODO]
  440. public override MembershipUserCollection FindUsersByEmail (string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
  441. {
  442. CheckParam ("emailToMatch", emailToMatch, 256);
  443. if (pageIndex < 0)
  444. throw new ArgumentException ("pageIndex must be >= 0");
  445. if (pageSize < 0)
  446. throw new ArgumentException ("pageSize must be >= 0");
  447. if (pageIndex * pageSize + pageSize - 1 > Int32.MaxValue)
  448. throw new ArgumentException ("pageIndex and pageSize are too large");
  449. string commandText;
  450. InitConnection();
  451. commandText = @"
  452. SELECT u.UserName, m.UserId, m.Email, m.PasswordQuestion, m.Comment, m.IsApproved,
  453. m.IsLockedOut, m.CreateDate, m.LastLoginDate, u.LastActivityDate,
  454. m.LastPasswordChangedDate, m.LastLockoutDate
  455. FROM dbo.aspnet_Membership m, dbo.aspnet_Applications a, dbo.aspnet_Users u
  456. WHERE m.ApplicationId = a.ApplicationId
  457. AND u.ApplicationId = a.ApplicationId
  458. AND m.UserId = u.UserId
  459. AND m.Email LIKE @Email
  460. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  461. DbCommand command = factory.CreateCommand ();
  462. command.CommandText = commandText;
  463. command.Connection = connection;
  464. command.CommandType = CommandType.Text;
  465. AddParameter (command, "Email", emailToMatch);
  466. AddParameter (command, "ApplicationName", ApplicationName);
  467. MembershipUserCollection c = BuildMembershipUserCollection (command, pageIndex, pageSize, out totalRecords);
  468. return c;
  469. }
  470. [MonoTODO]
  471. public override MembershipUserCollection FindUsersByName (string nameToMatch, int pageIndex, int pageSize, out int totalRecords)
  472. {
  473. CheckParam ("nameToMatch", nameToMatch, 256);
  474. if (pageIndex < 0)
  475. throw new ArgumentException ("pageIndex must be >= 0");
  476. if (pageSize < 0)
  477. throw new ArgumentException ("pageSize must be >= 0");
  478. if (pageIndex * pageSize + pageSize - 1 > Int32.MaxValue)
  479. throw new ArgumentException ("pageIndex and pageSize are too large");
  480. string commandText;
  481. InitConnection();
  482. commandText = @"
  483. SELECT u.UserName, m.UserId, m.Email, m.PasswordQuestion, m.Comment, m.IsApproved,
  484. m.IsLockedOut, m.CreateDate, m.LastLoginDate, u.LastActivityDate,
  485. m.LastPasswordChangedDate, m.LastLockoutDate
  486. FROM dbo.aspnet_Membership m, dbo.aspnet_Applications a, dbo.aspnet_Users u
  487. WHERE m.ApplicationId = a.ApplicationId
  488. AND u.ApplicationId = a.ApplicationId
  489. AND m.UserId = u.UserId
  490. AND u.UserName LIKE @UserName
  491. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  492. DbCommand command = factory.CreateCommand ();
  493. command.CommandText = commandText;
  494. command.Connection = connection;
  495. command.CommandType = CommandType.Text;
  496. AddParameter (command, "UserName", nameToMatch);
  497. AddParameter (command, "ApplicationName", ApplicationName);
  498. MembershipUserCollection c = BuildMembershipUserCollection (command, pageIndex, pageSize, out totalRecords);
  499. return c;
  500. }
  501. [MonoTODO]
  502. public override MembershipUserCollection GetAllUsers (int pageIndex, int pageSize, out int totalRecords)
  503. {
  504. if (pageIndex < 0)
  505. throw new ArgumentException ("pageIndex must be >= 0");
  506. if (pageSize < 0)
  507. throw new ArgumentException ("pageSize must be >= 0");
  508. if (pageIndex * pageSize + pageSize - 1 > Int32.MaxValue)
  509. throw new ArgumentException ("pageIndex and pageSize are too large");
  510. string commandText;
  511. InitConnection();
  512. commandText = @"
  513. SELECT u.UserName, m.UserId, m.Email, m.PasswordQuestion, m.Comment, m.IsApproved,
  514. m.IsLockedOut, m.CreateDate, m.LastLoginDate, u.LastActivityDate,
  515. m.LastPasswordChangedDate, m.LastLockoutDate
  516. FROM dbo.aspnet_Membership m, dbo.aspnet_Applications a, dbo.aspnet_Users u
  517. WHERE m.ApplicationId = a.ApplicationId
  518. AND u.ApplicationId = a.ApplicationId
  519. AND m.UserId = u.UserId
  520. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  521. DbCommand command = factory.CreateCommand ();
  522. command.CommandText = commandText;
  523. command.Connection = connection;
  524. command.CommandType = CommandType.Text;
  525. AddParameter (command, "ApplicationName", ApplicationName);
  526. MembershipUserCollection c = BuildMembershipUserCollection (command, pageIndex, pageSize, out totalRecords);
  527. return c;
  528. }
  529. MembershipUserCollection BuildMembershipUserCollection (DbCommand command, int pageIndex, int pageSize, out int totalRecords)
  530. {
  531. DbDataReader reader = null;
  532. try {
  533. int num_read = 0;
  534. int num_added = 0;
  535. int num_to_skip = pageIndex * pageSize;
  536. MembershipUserCollection users = new MembershipUserCollection ();
  537. reader = command.ExecuteReader ();
  538. while (reader.Read()) {
  539. if (num_read >= num_to_skip) {
  540. if (num_added < pageSize) {
  541. users.Add (GetUserFromReader (reader));
  542. num_added ++;
  543. }
  544. num_read ++;
  545. }
  546. }
  547. totalRecords = num_read;
  548. return users;
  549. }
  550. catch {
  551. totalRecords = 0;
  552. return null; /* should we let the exception through? */
  553. }
  554. finally {
  555. if (reader != null)
  556. reader.Close();
  557. }
  558. }
  559. [MonoTODO]
  560. public override int GetNumberOfUsersOnline ()
  561. {
  562. string commandText;
  563. InitConnection();
  564. commandText = @"
  565. SELECT COUNT (*)
  566. FROM dbo.aspnet_Membership m, dbo.aspnet_Applications a, dbo.aspnet_Users u
  567. WHERE m.ApplicationId = a.ApplicationId
  568. AND u.ApplicationId = a.ApplicationId
  569. AND m.UserId = u.UserId
  570. AND DATEADD(m,@UserIsOnlineTimeWindow,dbo.aspnet_Users.LastActivityDate) >= GETDATE()
  571. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  572. DbCommand command = factory.CreateCommand ();
  573. command.CommandText = commandText;
  574. command.Connection = connection;
  575. command.CommandType = CommandType.Text;
  576. AddParameter (command, "UserIsOnlineTimeWindow", userIsOnlineTimeWindow.Minutes.ToString());
  577. AddParameter (command, "ApplicationName", ApplicationName);
  578. try {
  579. return (int)command.ExecuteScalar ();
  580. }
  581. catch {
  582. return -1;
  583. }
  584. }
  585. [MonoTODO]
  586. public override string GetPassword (string username, string answer)
  587. {
  588. /* do the actual validation */
  589. /* if the validation succeeds:
  590. set LastLoginDate to DateTime.Now
  591. set FailedPasswordAnswerAttemptCount to 0
  592. set FailedPasswordAnswerAttemptWindowStart to DefaultDateTime
  593. */
  594. /* if validation fails:
  595. if (FailedPasswordAnswerAttemptWindowStart - DateTime.Now < PasswordAttemptWindow)
  596. increment FailedPasswordAnswerAttemptCount
  597. FailedPasswordAnswerAttemptWindowStart = DateTime.Now
  598. if (FailedPasswordAnswerAttemptCount > MaxInvalidPasswordAttempts)
  599. set IsLockedOut = true.
  600. set LastLockoutDate = DateTime.Now
  601. */
  602. throw new NotImplementedException ();
  603. }
  604. MembershipUser GetUserFromReader (DbDataReader reader)
  605. {
  606. return new MembershipUser (this.Name, /* XXX is this right? */
  607. reader.GetString (0), /* name */
  608. reader.GetGuid (1), /* providerUserKey */
  609. reader.IsDBNull (2) ? null : reader.GetString (2), /* email */
  610. reader.IsDBNull (3) ? null : reader.GetString (3), /* passwordQuestion */
  611. reader.IsDBNull (4) ? null : reader.GetString (4), /* comment */
  612. reader.GetBoolean (5), /* isApproved */
  613. reader.GetBoolean (6), /* isLockedOut */
  614. reader.GetDateTime (7), /* creationDate */
  615. reader.GetDateTime (8), /* lastLoginDate */
  616. reader.GetDateTime (9), /* lastActivityDate */
  617. reader.GetDateTime (10), /* lastPasswordChangedDate */
  618. reader.GetDateTime (11) /* lastLockoutDate */);
  619. }
  620. MembershipUser BuildMembershipUser (DbCommand query, bool userIsOnline)
  621. {
  622. DbDataReader reader = null;
  623. try {
  624. reader = query.ExecuteReader ();
  625. if (!reader.Read ())
  626. return null;
  627. MembershipUser user = GetUserFromReader (reader);
  628. if (user != null && userIsOnline) {
  629. string commandText;
  630. DbCommand command;
  631. commandText = @"
  632. UPDATE dbo.aspnet_Users u, dbo.aspnet_Application a
  633. SET u.LastActivityDate = GETDATE()
  634. WHERE u.ApplicationId = a.ApplicationId
  635. AND u.UserName = @UserName
  636. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  637. command = factory.CreateCommand ();
  638. command.CommandText = commandText;
  639. command.Connection = connection;
  640. command.CommandType = CommandType.Text;
  641. AddParameter (command, "UserName", user.UserName);
  642. AddParameter (command, "ApplicationName", ApplicationName);
  643. command.ExecuteNonQuery();
  644. }
  645. return user;
  646. }
  647. catch {
  648. return null; /* should we let the exception through? */
  649. }
  650. finally {
  651. if (reader != null)
  652. reader.Close ();
  653. }
  654. }
  655. [MonoTODO]
  656. public override MembershipUser GetUser (string username, bool userIsOnline)
  657. {
  658. CheckParam ("username", username, 256);
  659. string commandText;
  660. DbCommand command;
  661. InitConnection();
  662. commandText = @"
  663. SELECT u.UserName, m.UserId, m.Email, m.PasswordQuestion, m.Comment, m.IsApproved,
  664. m.IsLockedOut, m.CreateDate, m.LastLoginDate, u.LastActivityDate,
  665. m.LastPasswordChangedDate, m.LastLockoutDate
  666. FROM dbo.aspnet_Membership m, dbo.aspnet_Applications a, dbo.aspnet_Users u
  667. WHERE m.ApplicationId = a.ApplicationId
  668. AND u.ApplicationId = a.ApplicationId
  669. AND m.UserId = u.UserId
  670. AND u.UserName = @UserName
  671. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  672. command = factory.CreateCommand ();
  673. command.CommandText = commandText;
  674. command.Connection = connection;
  675. command.CommandType = CommandType.Text;
  676. AddParameter (command, "UserName", username);
  677. AddParameter (command, "ApplicationName", ApplicationName);
  678. MembershipUser u = BuildMembershipUser (command, userIsOnline);
  679. return u;
  680. }
  681. [MonoTODO]
  682. public override MembershipUser GetUser (object providerUserKey, bool userIsOnline)
  683. {
  684. string commandText;
  685. DbCommand command;
  686. InitConnection();
  687. commandText = @"
  688. SELECT u.UserName, m.UserId, m.Email, m.PasswordQuestion, m.Comment, m.IsApproved,
  689. m.IsLockedOut, m.CreateDate, m.LastLoginDate, u.LastActivityDate,
  690. m.LastPasswordChangedDate, m.LastLockoutDate
  691. FROM dbo.aspnet_Membership m, dbo.aspnet_Applications a, dbo.aspnet_Users u
  692. WHERE m.ApplicationId = a.ApplicationId
  693. AND u.ApplicationId = a.ApplicationId
  694. AND m.UserId = u.UserId
  695. AND u.UserId = @UserKey
  696. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  697. command = factory.CreateCommand ();
  698. command.CommandText = commandText;
  699. command.Connection = connection;
  700. command.CommandType = CommandType.Text;
  701. AddParameter (command, "UserKey", providerUserKey.ToString());
  702. AddParameter (command, "ApplicationName", ApplicationName);
  703. MembershipUser u = BuildMembershipUser (command, userIsOnline);
  704. return u;
  705. }
  706. [MonoTODO]
  707. public override string GetUserNameByEmail (string email)
  708. {
  709. CheckParam ("email", email, 256);
  710. string commandText;
  711. DbCommand command;
  712. InitConnection();
  713. commandText = @"
  714. SELECT u.UserName
  715. FROM dbo.aspnet_Membership m, dbo.aspnet_Applications a, dbo.aspnet_Users u
  716. WHERE m.ApplicationId = a.ApplicationId
  717. AND u.ApplicationId = a.ApplicationId
  718. AND m.UserId = u.UserId
  719. AND m.Email = @Email
  720. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  721. command = factory.CreateCommand ();
  722. command.CommandText = commandText;
  723. command.Connection = connection;
  724. command.CommandType = CommandType.Text;
  725. AddParameter (command, "Email", email);
  726. AddParameter (command, "ApplicationName", ApplicationName);
  727. try {
  728. DbDataReader reader = command.ExecuteReader ();
  729. string rv = null;
  730. while (reader.Read())
  731. rv = reader.GetString(0);
  732. reader.Close();
  733. return rv;
  734. }
  735. catch {
  736. return null; /* should we allow the exception through? */
  737. }
  738. }
  739. bool GetBoolConfigValue (NameValueCollection config, string name, bool def)
  740. {
  741. bool rv = def;
  742. string val = config[name];
  743. if (val != null) {
  744. try { rv = Boolean.Parse (val); }
  745. catch (Exception e) {
  746. throw new ProviderException (String.Format ("{0} must be true or false", name), e); }
  747. }
  748. return rv;
  749. }
  750. int GetIntConfigValue (NameValueCollection config, string name, int def)
  751. {
  752. int rv = def;
  753. string val = config[name];
  754. if (val != null) {
  755. try { rv = Int32.Parse (val); }
  756. catch (Exception e) {
  757. throw new ProviderException (String.Format ("{0} must be an integer", name), e); }
  758. }
  759. return rv;
  760. }
  761. int GetEnumConfigValue (NameValueCollection config, string name, Type enumType, int def)
  762. {
  763. int rv = def;
  764. string val = config[name];
  765. if (val != null) {
  766. try { rv = (int)Enum.Parse (enumType, val); }
  767. catch (Exception e) {
  768. throw new ProviderException (String.Format ("{0} must be one of the following values: {1}", name, String.Join (",", Enum.GetNames (enumType))), e); }
  769. }
  770. return rv;
  771. }
  772. string GetStringConfigValue (NameValueCollection config, string name, string def)
  773. {
  774. string rv = def;
  775. string val = config[name];
  776. if (val != null)
  777. rv = val;
  778. return rv;
  779. }
  780. public override void Initialize (string name, NameValueCollection config)
  781. {
  782. if (config == null)
  783. throw new ArgumentNullException ("config");
  784. base.Initialize (name, config);
  785. applicationName = GetStringConfigValue (config, "applicationName", "/");
  786. enablePasswordReset = GetBoolConfigValue (config, "enablePasswordReset", true);
  787. enablePasswordRetrieval = GetBoolConfigValue (config, "enablePasswordRetrieval", false);
  788. requiresQuestionAndAnswer = GetBoolConfigValue (config, "requiresQuestionAndAnswer", true);
  789. requiresUniqueEmail = GetBoolConfigValue (config, "requiresUniqueEmail", false);
  790. passwordFormat = (MembershipPasswordFormat)GetEnumConfigValue (config, "passwordFormat", typeof (MembershipPasswordFormat),
  791. (int)MembershipPasswordFormat.Hashed);
  792. maxInvalidPasswordAttempts = GetIntConfigValue (config, "maxInvalidPasswordAttempts", 5);
  793. minRequiredPasswordLength = GetIntConfigValue (config, "minRequiredPasswordLength", 7);
  794. minRequiredNonAlphanumericCharacters = GetIntConfigValue (config, "minRequiredNonAlphanumericCharacters", 1);
  795. passwordAttemptWindow = GetIntConfigValue (config, "passwordAttemptWindow", 10);
  796. passwordStrengthRegularExpression = GetStringConfigValue (config, "passwordStrengthRegularExpression", "");
  797. MembershipSection section = (MembershipSection)WebConfigurationManager.GetSection ("system.web/membership");
  798. userIsOnlineTimeWindow = section.UserIsOnlineTimeWindow;
  799. /* come up with an init_vector for encryption algorithms */
  800. // IV is 8 bytes long for 3DES
  801. init_vector = new byte[8];
  802. int len = applicationName.Length;
  803. for (int i = 0; i < 8; i++) {
  804. if (i >= len)
  805. break;
  806. init_vector [i] = (byte) applicationName [i];
  807. }
  808. string connectionStringName = config["connectionStringName"];
  809. if (applicationName.Length > 256)
  810. throw new ProviderException ("The ApplicationName attribute must be 256 characters long or less.");
  811. if (connectionStringName == null || connectionStringName.Length == 0)
  812. throw new ProviderException ("The ConnectionStringName attribute must be present and non-zero length.");
  813. connectionString = WebConfigurationManager.ConnectionStrings[connectionStringName];
  814. }
  815. [MonoTODO]
  816. public override string ResetPassword (string username, string answer)
  817. {
  818. throw new NotImplementedException ();
  819. }
  820. public override void UpdateUser (MembershipUser user)
  821. {
  822. if (user == null) throw new ArgumentNullException ("user");
  823. if (user.UserName == null) throw new ArgumentNullException ("user.UserName");
  824. if (RequiresUniqueEmail && user.Email == null) throw new ArgumentNullException ("user.Email");
  825. CheckParam ("user.UserName", user.UserName, 256);
  826. if (user.Email.Length > 256 || (RequiresUniqueEmail && user.Email.Length == 0))
  827. throw new ArgumentException ("invalid format for user.Email");
  828. DbTransaction trans = connection.BeginTransaction ();
  829. string commandText;
  830. DbCommand command;
  831. InitConnection();
  832. try {
  833. DateTime now = DateTime.Now.ToUniversalTime ();
  834. commandText = String.Format (@"
  835. UPDATE m
  836. SET Email = {0},
  837. Comment = {1},
  838. IsApproved = @IsApproved,
  839. LastLoginDate = @Now
  840. FROM dbo.aspnet_Membership m, dbo.aspnet_Users u, dbo.aspnet_Applications a
  841. WHERE m.ApplicationId = a.ApplicationId
  842. AND u.ApplicationId = a.ApplicationId
  843. AND m.UserId = u.UserId
  844. AND u.LoweredUserName = LOWER(@UserName)
  845. AND a.LoweredApplicationName = LOWER(@ApplicationName)",
  846. user.Email == null ? "NULL" : "@Email",
  847. user.Comment == null ? "NULL" : "@Comment");
  848. command = factory.CreateCommand ();
  849. command.Transaction = trans;
  850. command.CommandText = commandText;
  851. command.Connection = connection;
  852. command.CommandType = CommandType.Text;
  853. if (user.Email != null)
  854. AddParameter (command, "Email", user.Email);
  855. if (user.Comment != null)
  856. AddParameter (command, "Comment", user.Comment);
  857. AddParameter (command, "IsApproved", user.IsApproved.ToString());
  858. AddParameter (command, "UserName", user.UserName);
  859. AddParameter (command, "ApplicationName", ApplicationName);
  860. AddParameter (command, "Now", now.ToString ());
  861. if (0 == command.ExecuteNonQuery())
  862. throw new ProviderException ("failed to membership table");
  863. commandText = @"
  864. UPDATE dbo.aspnet_Users
  865. SET LastActivityDate = @Now
  866. FROM dbo.aspnet_Users u, dbo.aspnet_Applications a
  867. WHERE a.ApplicationId = a.ApplicationId
  868. AND u.LoweredUserName = LOWER(@UserName)
  869. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  870. command = factory.CreateCommand ();
  871. command.Transaction = trans;
  872. command.CommandText = commandText;
  873. command.Connection = connection;
  874. command.CommandType = CommandType.Text;
  875. AddParameter (command, "UserName", user.UserName);
  876. AddParameter (command, "ApplicationName", ApplicationName);
  877. AddParameter (command, "Now", now.ToString ());
  878. if (0 == command.ExecuteNonQuery())
  879. throw new ProviderException ("failed to user table");
  880. trans.Commit ();
  881. }
  882. catch (ProviderException e) {
  883. trans.Rollback ();
  884. throw e;
  885. }
  886. catch (Exception e) {
  887. trans.Rollback ();
  888. throw new ProviderException ("failed to update user", e);
  889. }
  890. }
  891. [MonoTODO ("flesh out the case where validation fails")]
  892. public override bool ValidateUser (string username, string password)
  893. {
  894. MembershipUser user = GetUser (username, false);
  895. /* if the user is locked out, return false immediately */
  896. if (user.IsLockedOut)
  897. return false;
  898. /* if the user is not yet approved, return false */
  899. if (!user.IsApproved)
  900. return false;
  901. ValidatePasswordEventArgs args = new ValidatePasswordEventArgs (username, password, false);
  902. OnValidatingPassword (args);
  903. if (args.Cancel)
  904. throw new ProviderException ("Password validation failed");
  905. if (args.FailureInformation != null)
  906. throw args.FailureInformation;
  907. /* get the password/salt from the db */
  908. string db_password;
  909. MembershipPasswordFormat db_passwordFormat;
  910. string db_salt;
  911. DbTransaction trans = connection.BeginTransaction ();
  912. string commandText;
  913. DbCommand command;
  914. InitConnection();
  915. try {
  916. commandText = @"
  917. SELECT m.Password, m.PasswordFormat, m.PasswordSalt
  918. FROM dbo.aspnet_Membership m, dbo.aspnet_Applications a, dbo.aspnet_Users u
  919. WHERE m.ApplicationId = a.ApplicationId
  920. AND u.ApplicationId = a.ApplicationId
  921. AND m.UserId = u.UserId
  922. AND u.LoweredUserName = LOWER(@UserName)
  923. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  924. command = factory.CreateCommand ();
  925. command.Transaction = trans;
  926. command.CommandText = commandText;
  927. command.Connection = connection;
  928. command.CommandType = CommandType.Text;
  929. AddParameter (command, "UserName", user.UserName);
  930. AddParameter (command, "ApplicationName", ApplicationName);
  931. DbDataReader reader = command.ExecuteReader ();
  932. reader.Read ();
  933. db_password = reader.GetString (0);
  934. db_passwordFormat = (MembershipPasswordFormat)reader.GetInt32 (1);
  935. db_salt = reader.GetString (2);
  936. reader.Close();
  937. /* do the actual validation */
  938. switch (db_passwordFormat) {
  939. case MembershipPasswordFormat.Hashed:
  940. byte[] salt = Convert.FromBase64String (db_salt);
  941. password = HashAndBase64Encode (password, salt);
  942. break;
  943. case MembershipPasswordFormat.Encrypted:
  944. password = EncryptAndBase64Encode (password);
  945. break;
  946. case MembershipPasswordFormat.Clear:
  947. break;
  948. }
  949. bool valid = (password == db_password);
  950. if (valid) {
  951. DateTime now = DateTime.Now.ToUniversalTime ();
  952. /* if the validation succeeds:
  953. set LastLoginDate to DateTime.Now
  954. set FailedPasswordAttemptCount to 0
  955. set FailedPasswordAttemptWindow to DefaultDateTime
  956. set FailedPasswordAnswerAttemptCount to 0
  957. set FailedPasswordAnswerAttemptWindowStart to DefaultDateTime
  958. */
  959. commandText = @"
  960. UPDATE dbo.aspnet_Membership
  961. SET LastLoginDate = @Now,
  962. FailedPasswordAttemptCount = 0,
  963. FailedPasswordAttemptWindowStart = @DefaultDateTime,
  964. FailedPasswordAnswerAttemptCount = 0,
  965. FailedPasswordAnswerAttemptWindowStart = @DefaultDateTime
  966. FROM dbo.aspnet_Membership m, dbo.aspnet_Applications a, dbo.aspnet_Users u
  967. WHERE m.ApplicationId = a.ApplicationId
  968. AND u.ApplicationId = a.ApplicationId
  969. AND m.UserId = u.UserId
  970. AND u.LoweredUserName = LOWER(@UserName)
  971. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  972. command = factory.CreateCommand ();
  973. command.Transaction = trans;
  974. command.CommandText = commandText;
  975. command.Connection = connection;
  976. command.CommandType = CommandType.Text;
  977. AddParameter (command, "UserName", user.UserName);
  978. AddParameter (command, "ApplicationName", ApplicationName);
  979. AddParameter (command, "Now", now.ToString ());
  980. AddParameter (command, "DefaultDateTime", DefaultDateTime.ToString());
  981. if (1 != (int)command.ExecuteNonQuery ())
  982. throw new ProviderException ("failed to update Membership table");
  983. commandText = @"
  984. UPDATE dbo.aspnet_Users
  985. SET LastActivityDate = @Now
  986. FROM dbo.aspnet_Applications a, dbo.aspnet_Users u
  987. WHERE u.ApplicationId = a.ApplicationId
  988. AND u.LoweredUserName = LOWER(@UserName)
  989. AND a.LoweredApplicationName = LOWER(@ApplicationName)";
  990. command = factory.CreateCommand ();
  991. command.Transaction = trans;
  992. command.CommandText = commandText;
  993. command.Connection = connection;
  994. command.CommandType = CommandType.Text;
  995. AddParameter (command, "UserName", user.UserName);
  996. AddParameter (command, "ApplicationName", ApplicationName);
  997. AddParameter (command, "Now", now.ToString ());
  998. if (1 != (int)command.ExecuteNonQuery ())
  999. throw new ProviderException ("failed to update User table");
  1000. }
  1001. else {
  1002. /* if validation fails:
  1003. if (FailedPasswordAttemptWindowStart - DateTime.Now < PasswordAttemptWindow)
  1004. increment FailedPasswordAttemptCount
  1005. FailedPasswordAttemptWindowStart = DateTime.Now
  1006. if (FailedPasswordAttemptCount > MaxInvalidPasswordAttempts)
  1007. set IsLockedOut = true.
  1008. set LastLockoutDate = DateTime.Now
  1009. */
  1010. }
  1011. trans.Commit ();
  1012. return valid;
  1013. }
  1014. catch {
  1015. trans.Rollback ();
  1016. return false; /* should we allow the exception through? */
  1017. }
  1018. }
  1019. [MonoTODO]
  1020. public override bool UnlockUser (string userName)
  1021. {
  1022. string commandText = @"
  1023. UPDATE dbo.aspnet_Membership, dbo.aspnet_Users, dbo.aspnet_Application
  1024. SET dbo.aspnet_Membership.IsLockedOut = 0
  1025. WHERE dbo.aspnet_Membership.UserId = dbo.aspnet_Users.UserId
  1026. AND dbo.aspnet_Membership.ApplicationId = dbo.aspnet_Applications.ApplicationId
  1027. AND dbo.aspnet_Users.ApplicationId = dbo.aspnet_Applications.ApplicationId
  1028. AND dbo.aspnet_Users.LoweredUserName = LOWER (@UserName)
  1029. AND dbo.aspnet_Applications.LoweredApplicationName = LOWER(@ApplicationName)";
  1030. CheckParam ("userName", userName, 256);
  1031. InitConnection();
  1032. DbCommand command = factory.CreateCommand ();
  1033. command.CommandText = commandText;
  1034. command.Connection = connection;
  1035. command.CommandType = CommandType.Text;
  1036. AddParameter (command, "UserName", userName);
  1037. AddParameter (command, "ApplicationName", ApplicationName);
  1038. try {
  1039. return command.ExecuteNonQuery() == 1;
  1040. }
  1041. catch {
  1042. return false;
  1043. }
  1044. }
  1045. [MonoTODO]
  1046. public override string ApplicationName {
  1047. get { return applicationName; }
  1048. set { applicationName = value; }
  1049. }
  1050. public override bool EnablePasswordReset {
  1051. get { return enablePasswordReset; }
  1052. }
  1053. public override bool EnablePasswordRetrieval {
  1054. get { return enablePasswordRetrieval; }
  1055. }
  1056. public override MembershipPasswordFormat PasswordFormat {
  1057. get { return passwordFormat; }
  1058. }
  1059. public override bool RequiresQuestionAndAnswer {
  1060. get { return requiresQuestionAndAnswer; }
  1061. }
  1062. public override bool RequiresUniqueEmail {
  1063. get { return requiresUniqueEmail; }
  1064. }
  1065. public override int MaxInvalidPasswordAttempts {
  1066. get { return maxInvalidPasswordAttempts; }
  1067. }
  1068. public override int MinRequiredNonAlphanumericCharacters {
  1069. get { return minRequiredNonAlphanumericCharacters; }
  1070. }
  1071. public override int MinRequiredPasswordLength {
  1072. get { return minRequiredPasswordLength; }
  1073. }
  1074. public override int PasswordAttemptWindow {
  1075. get { return passwordAttemptWindow; }
  1076. }
  1077. public override string PasswordStrengthRegularExpression {
  1078. get { return passwordStrengthRegularExpression; }
  1079. }
  1080. }
  1081. }
  1082. #endif