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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
|
// Copyright (c) 2004-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
//
// MySQL Connector/NET is licensed under the terms of the GPLv2
// <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
// MySQL Connectors. There are special exceptions to the terms and
// conditions of the GPLv2 as it is applied to this software, see the
// FLOSS License Exception
// <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
// This code was contributed by Sean Wright (srwright@alcor.concordia.ca) on 2007-01-12
// The copyright was assigned and transferred under the terms of
// the MySQL Contributor License Agreement (CLA)
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Configuration.Provider;
using System.Data;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.Web.Configuration;
using System.Web.Hosting;
using System.Web.Security;
using MySql.Data.MySqlClient;
using MySql.Web.Properties;
using MySql.Web.Profile;
using MySql.Web.Common;
using System.Text.RegularExpressions;
using MySql.Web.General;
namespace MySql.Web.Security
{
/// <summary>
/// Manages storage of membership information for an ASP.NET application in a MySQL database.
/// </summary>
/// <remarks>
/// <para>
/// This class is used by the <see cref="Membership"/> and <see cref="MembershipUser"/> classes
/// to provide membership services for ASP.NET applications using a MySQL database.
/// </para>
/// </remarks>
/// <example>
/// <code source="CodeExamples/MembershipCodeExample2.xml"/>
/// </example>
public sealed class MySQLMembershipProvider : MembershipProvider
{
private int newPasswordLength = 8;
private string eventSource = "MySQLMembershipProvider";
private string eventLog = "Application";
private string exceptionMessage = "An exception occurred. Please check the Event Log.";
private string connectionString;
private int minRequiredPasswordLength;
private bool writeExceptionsToEventLog;
private bool enablePasswordReset;
private bool enablePasswordRetrieval;
private bool requiresQuestionAndAnswer;
private bool requiresUniqueEmail;
private int maxInvalidPasswordAttempts;
private int passwordAttemptWindow;
private MembershipPasswordFormat passwordFormat;
private int minRequiredNonAlphanumericCharacters;
private string passwordStrengthRegularExpression;
private Application app;
/// <summary>
/// Initializes the MySQL membership provider with the property values specified in the
/// ASP.NET application's configuration file. This method is not intended to be used directly
/// from your code.
/// </summary>
/// <param name="name">The name of the <see cref="MySQLMembershipProvider"/> instance to initialize.</param>
/// <param name="config">A collection of the name/value pairs representing the
/// provider-specific attributes specified in the configuration for this provider.</param>
/// <exception cref="T:System.ArgumentNullException">config is a null reference.</exception>
/// <exception cref="T:System.InvalidOperationException">An attempt is made to call <see cref="M:System.Configuration.Provider.ProviderBase.Initialize(System.String,System.Collections.Specialized.NameValueCollection)"/> on a provider after the provider has already been initialized.</exception>
/// <exception cref="T:System.Configuration.Provider.ProviderException"></exception>
public override void Initialize(string name, NameValueCollection config)
{
if (config == null)
{
throw new ArgumentNullException("config");
}
if (name == null || name.Length == 0)
{
name = "MySQLMembershipProvider";
}
if (string.IsNullOrEmpty(config["description"]))
{
config.Remove("description");
config.Add("description", "MySQL default application");
}
base.Initialize(name, config);
string applicationName = GetConfigValue(config["applicationName"],
HostingEnvironment.ApplicationVirtualPath);
maxInvalidPasswordAttempts = Convert.ToInt32(GetConfigValue(config["maxInvalidPasswordAttempts"], "5"));
passwordAttemptWindow = Convert.ToInt32(GetConfigValue(config["passwordAttemptWindow"], "10"));
minRequiredNonAlphanumericCharacters =
Convert.ToInt32(GetConfigValue(config["minRequiredNonalphanumericCharacters"], "1"));
minRequiredPasswordLength = Convert.ToInt32(GetConfigValue(config["minRequiredPasswordLength"], "7"));
passwordStrengthRegularExpression =
Convert.ToString(GetConfigValue(config["passwordStrengthRegularExpression"], ""));
enablePasswordReset = Convert.ToBoolean(GetConfigValue(config["enablePasswordReset"], "True"));
enablePasswordRetrieval = Convert.ToBoolean(
GetConfigValue(config["enablePasswordRetrieval"], "False"));
requiresQuestionAndAnswer = Convert.ToBoolean(GetConfigValue(config["requiresQuestionAndAnswer"], "False"));
requiresUniqueEmail = Convert.ToBoolean(GetConfigValue(config["requiresUniqueEmail"], "True"));
writeExceptionsToEventLog = Convert.ToBoolean(GetConfigValue(config["writeExceptionsToEventLog"], "True"));
string temp_format = config["passwordFormat"];
if (temp_format == null)
temp_format = "hashed";
else
temp_format = temp_format.ToLowerInvariant();
if (temp_format == "hashed")
passwordFormat = MembershipPasswordFormat.Hashed;
else if (temp_format == "encrypted")
passwordFormat = MembershipPasswordFormat.Encrypted;
else if (temp_format == "clear")
passwordFormat = MembershipPasswordFormat.Clear;
else
throw new ProviderException("Password format not supported.");
// if the user is asking for the ability to retrieve hashed passwords, then let
// them know we can't
if (PasswordFormat == MembershipPasswordFormat.Hashed)
{
if (EnablePasswordRetrieval)
throw new ProviderException(Resources.CannotRetrieveHashedPasswords);
if (Runtime.IsMono)
throw new ProviderException(Resources.MonoDoesNotSupportHash);
}
ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings[
config["connectionStringName"]];
if (ConnectionStringSettings != null)
connectionString = ConnectionStringSettings.ConnectionString.Trim();
else
connectionString = "";
if (String.IsNullOrEmpty(connectionString)) return;
// make sure we have the correct schema
SchemaManager.CheckSchema(connectionString, config);
app = new Application(applicationName, base.Description);
}
private static string GetConfigValue(string configValue, string defaultValue)
{
if (string.IsNullOrEmpty(configValue))
{
return defaultValue;
}
return configValue;
}
#region Properties
/// <summary>
/// The name of the application using the MySQL membership provider.
/// </summary>
/// <value>The name of the application using the MySQL membership provider. The default is the
/// application virtual path.</value>
/// <remarks>The ApplicationName is used by the MySqlMembershipProvider to separate
/// membership information for multiple applications. Using different application names,
/// applications can use the same membership database.
/// Likewise, multiple applications can make use of the same membership data by simply using
/// the same application name.
/// Caution should be taken with multiple applications as the ApplicationName property is not
/// thread safe during writes.
/// </remarks>
/// <example>
/// The following example shows the membership element being used in an applications web.config file.
/// The application name setting is being used.
/// <code source="CodeExamples/MembershipCodeExample1.xml"/>
/// </example>
public override string ApplicationName
{
get { return app.Name; }
set
{
lock (this)
{
if (value.ToLowerInvariant() == app.Name.ToLowerInvariant()) return;
app = new Application(value, String.Empty);
}
}
}
/// <summary>
/// Indicates whether the membership provider is configured to allow users to reset their passwords.
/// </summary>
/// <value>true if the membership provider supports password reset; otherwise, false. The default is true.</value>
/// <remarks>Allows the user to replace their password with a new, randomly generated password.
/// This can be especially handy when using hashed passwords since hashed passwords cannot be
/// retrieved.</remarks>
/// <example>
/// The following example shows the membership element being used in an applications web.config file.
/// <code source="CodeExamples/MembershipCodeExample1.xml"/>
/// </example>
public override bool EnablePasswordReset
{
get { return enablePasswordReset; }
}
/// <summary>
/// Indicates whether the membership provider is configured to allow users to retrieve
/// their passwords.
/// </summary>
/// <value>true if the membership provider is configured to support password retrieval;
/// otherwise, false. The default is false.</value>
/// <remarks>If the system is configured to use hashed passwords, then retrieval is not possible.
/// If the user attempts to initialize the provider with hashed passwords and enable password retrieval
/// set to true then a <see cref="ProviderException"/> is thrown.</remarks>
/// <example>
/// The following example shows the membership element being used in an applications web.config file.
/// <code source="CodeExamples/MembershipCodeExample1.xml"/>
/// </example>
public override bool EnablePasswordRetrieval
{
get { return enablePasswordRetrieval; }
}
/// <summary>
/// Gets a value indicating whether the membership provider is
/// configured to require the user to answer a password question
/// for password reset and retrieval.
/// </summary>
/// <value>true if a password answer is required for password
/// reset and retrieval; otherwise, false. The default is false.</value>
/// <example>
/// The following example shows the membership element being used in an applications web.config file.
/// <code source="CodeExamples/MembershipCodeExample1.xml"/>
/// </example>
public override bool RequiresQuestionAndAnswer
{
get { return requiresQuestionAndAnswer; }
}
/// <summary>
/// Gets a value indicating whether the membership provider is configured
/// to require a unique e-mail address for each user name.
/// </summary>
/// <value>true if the membership provider requires a unique e-mail address;
/// otherwise, false. The default is true.</value>
/// <example>
/// The following example shows the membership element being used in an applications web.config file.
/// <code source="CodeExamples/MembershipCodeExample1.xml"/>
/// </example>
public override bool RequiresUniqueEmail
{
get { return requiresUniqueEmail; }
}
/// <summary>
/// Gets the number of invalid password or password-answer attempts allowed
/// before the membership user is locked out.
/// </summary>
/// <value>The number of invalid password or password-answer attempts allowed
/// before the membership user is locked out.</value>
/// <example>
/// The following example shows the membership element being used in an applications web.config file.
/// <code source="CodeExamples/MembershipCodeExample1.xml"/>
/// </example>
public override int MaxInvalidPasswordAttempts
{
get { return maxInvalidPasswordAttempts; }
}
/// <summary>
/// Gets the number of minutes in which a maximum number of invalid password or
/// password-answer attempts are allowed before the membership user is locked out.
/// </summary>
/// <value>The number of minutes in which a maximum number of invalid password or
/// password-answer attempts are allowed before the membership user is locked out.</value>
/// <example>
/// The following example shows the membership element being used in an applications web.config file.
/// <code source="CodeExamples/MembershipCodeExample1.xml"/>
/// </example>
public override int PasswordAttemptWindow
{
get { return passwordAttemptWindow; }
}
/// <summary>
/// Gets a value indicating the format for storing passwords in the membership data store.
/// </summary>
/// <value>One of the <see cref="T:System.Web.Security.MembershipPasswordFormat"/>
/// values indicating the format for storing passwords in the data store.</value>
/// <example>
/// The following example shows the membership element being used in an applications web.config file.
/// <code source="CodeExamples/MembershipCodeExample1.xml"/>
/// </example>
public override MembershipPasswordFormat PasswordFormat
{
get { return passwordFormat; }
}
/// <summary>
/// Gets the minimum number of special characters that must be present in a valid password.
/// </summary>
/// <value>The minimum number of special characters that must be present
/// in a valid password.</value>
/// <example>
/// The following example shows the membership element being used in an applications web.config file.
/// <code source="CodeExamples/MembershipCodeExample1.xml"/>
/// </example>
public override int MinRequiredNonAlphanumericCharacters
{
get { return minRequiredNonAlphanumericCharacters; }
}
/// <summary>
/// Gets the minimum length required for a password.
/// </summary>
/// <value>The minimum length required for a password. </value>
/// <example>
/// The following example shows the membership element being used in an applications web.config file.
/// <code source="CodeExamples/MembershipCodeExample1.xml"/>
/// </example>
public override int MinRequiredPasswordLength
{
get { return minRequiredPasswordLength; }
}
/// <summary>
/// Gets the regular expression used to evaluate a password.
/// </summary>
/// <value>A regular expression used to evaluate a password.</value>
/// <example>
/// The following example shows the membership element being used in an applications web.config file.
/// In this example, the regular expression specifies that the password must meet the following
/// criteria:
/// <ul>
/// <list>Is at least seven characters.</list>
/// <list>Contains at least one digit.</list>
/// <list>Contains at least one special (non-alphanumeric) character.</list>
/// </ul>
/// <code source="CodeExamples/MembershipCodeExample1.xml"/>
/// </example>
public override string PasswordStrengthRegularExpression
{
get { return passwordStrengthRegularExpression; }
}
/// <summary>
/// Gets or sets a value indicating whether exceptions are written to the event log.
/// </summary>
/// <value>
/// <c>true</c> if exceptions should be written to the log; otherwise, <c>false</c>.
/// </value>
public bool WriteExceptionsToEventLog
{
get { return writeExceptionsToEventLog; }
set { writeExceptionsToEventLog = value; }
}
#endregion
#region Public Methods
/// <summary>
/// Changes the password.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="oldPassword">The old password.</param>
/// <param name="newPassword">The new password.</param>
/// <returns>true if the password was updated successfully, false if the supplied old password
/// is invalid, the user is locked out, or the user does not exist in the database.</returns>
public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
// this will return false if the username doesn't exist
if (!(ValidateUser(username, oldPassword)))
return false;
ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, newPassword, true);
OnValidatingPassword(args);
if (args.Cancel)
{
if (!(args.FailureInformation == null))
throw args.FailureInformation;
else
throw new ProviderException(Resources.ChangePasswordCanceled);
}
// validate the password according to current guidelines
if (!ValidatePassword(newPassword, "newPassword", true))
return false;
try
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
// retrieve the existing key and format for this user
string passwordKey;
MembershipPasswordFormat passwordFormat;
int userId = GetUserId(connection, username);
GetPasswordInfo(connection, userId, out passwordKey, out passwordFormat);
MySqlCommand cmd = new MySqlCommand(
@"UPDATE my_aspnet_Membership
SET Password = @pass, LastPasswordChangedDate = @lastPasswordChangedDate
WHERE userId=@userId", connection);
cmd.Parameters.AddWithValue("@pass",
EncodePassword(newPassword, passwordKey, passwordFormat));
cmd.Parameters.AddWithValue("@lastPasswordChangedDate", DateTime.Now);
cmd.Parameters.AddWithValue("@userId", userId);
return cmd.ExecuteNonQuery() > 0;
}
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "ChangePassword");
throw new ProviderException(exceptionMessage, e);
}
}
/// <summary>
/// Changes the password question and answer.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
/// <param name="newPwdQuestion">The new password question.</param>
/// <param name="newPwdAnswer">The new password answer.</param>
/// <returns>true if the update was successful; otherwise, false. A value of false is
/// also returned if the password is incorrect, the user is locked out, or the user
/// does not exist in the database.</returns>
public override bool ChangePasswordQuestionAndAnswer(string username,
string password, string newPwdQuestion, string newPwdAnswer)
{
// this handles the case where the username doesn't exist
if (!(ValidateUser(username, password)))
return false;
try
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string passwordKey;
MembershipPasswordFormat passwordFormat;
int userId = GetUserId(connection, username);
GetPasswordInfo(connection, userId, out passwordKey, out passwordFormat);
MySqlCommand cmd = new MySqlCommand(
@"UPDATE my_aspnet_Membership
SET PasswordQuestion = @passwordQuestion, PasswordAnswer = @passwordAnswer
WHERE userId=@userId", connection);
cmd.Parameters.AddWithValue("@passwordQuestion", newPwdQuestion);
cmd.Parameters.AddWithValue("@passwordAnswer",
EncodePassword(newPwdAnswer, passwordKey, passwordFormat));
cmd.Parameters.AddWithValue("@userId", userId);
return cmd.ExecuteNonQuery() > 0;
}
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "ChangePasswordQuestionAndAnswer");
throw new ProviderException(exceptionMessage, e);
}
}
/// <summary>
/// Adds a new membership user to the data source.
/// </summary>
/// <param name="username">The user name for the new user.</param>
/// <param name="password">The password for the new user.</param>
/// <param name="email">The e-mail address for the new user.</param>
/// <param name="passwordQuestion">The password question for the new user.</param>
/// <param name="passwordAnswer">The password answer for the new user</param>
/// <param name="isApproved">Whether or not the new user is approved to be validated.</param>
/// <param name="providerUserKey">The unique identifier from the membership data source for the user.</param>
/// <param name="status">A <see cref="T:System.Web.Security.MembershipCreateStatus"/> enumeration value indicating whether the user was created successfully.</param>
/// <returns>
/// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the information for the newly created user.
/// </returns>
public override MembershipUser CreateUser(string username, string password,
string email, string passwordQuestion, string passwordAnswer,
bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
ValidatePasswordEventArgs Args = new ValidatePasswordEventArgs(username, password, true);
OnValidatingPassword(Args);
if (Args.Cancel)
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
if (RequiresUniqueEmail && !String.IsNullOrEmpty(GetUserNameByEmail(email)))
{
status = MembershipCreateStatus.DuplicateEmail;
return null;
}
ValidateQA(passwordQuestion, passwordAnswer);
// now try to validate the password
if (!ValidatePassword(password, "password", false))
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
// now check to see if we already have a member by this name
MembershipUser u = GetUser(username, false);
if (u != null)
{
status = MembershipCreateStatus.DuplicateUserName;
return null;
}
string passwordKey = GetPasswordKey();
DateTime createDate = DateTime.Now;
MySqlTransaction transaction = null;
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
try
{
connection.Open();
transaction = connection.BeginTransaction();
// either create a new user or fetch the existing user id
int userId = SchemaManager.CreateOrFetchUserId(connection, username,
app.EnsureId(connection), true);
MySqlCommand cmd = new MySqlCommand(
@"INSERT INTO my_aspnet_Membership
VALUES(@userId, @email, @comment, @password, @passwordKey,
@passwordFormat, @passwordQuestion, @passwordAnswer,
@isApproved, @lastActivityDate, @lastLoginDate,
@lastPasswordChangedDate, @creationDate,
@isLockedOut, @lastLockedOutDate, @failedPasswordAttemptCount,
@failedPasswordAttemptWindowStart, @failedPasswordAnswerAttemptCount,
@failedPasswordAnswerAttemptWindowStart)",
connection);
cmd.Parameters.AddWithValue("@userId", userId);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@comment", "");
cmd.Parameters.AddWithValue("@password",
EncodePassword(password, passwordKey, PasswordFormat));
cmd.Parameters.AddWithValue("@passwordKey", passwordKey);
cmd.Parameters.AddWithValue("@passwordFormat", PasswordFormat);
cmd.Parameters.AddWithValue("@passwordQuestion", passwordQuestion);
cmd.Parameters.AddWithValue("@passwordAnswer",
EncodePassword(passwordAnswer, passwordKey, PasswordFormat));
cmd.Parameters.AddWithValue("@isApproved", isApproved);
cmd.Parameters.AddWithValue("@lastActivityDate", createDate);
cmd.Parameters.AddWithValue("@lastLoginDate", createDate);
cmd.Parameters.AddWithValue("@lastPasswordChangedDate", createDate);
cmd.Parameters.AddWithValue("@creationDate", createDate);
cmd.Parameters.AddWithValue("@isLockedOut", false);
cmd.Parameters.AddWithValue("@lastLockedOutDate", createDate);
cmd.Parameters.AddWithValue("@failedPasswordAttemptCount", 0);
cmd.Parameters.AddWithValue("@failedPasswordAttemptWindowStart", createDate);
cmd.Parameters.AddWithValue("@failedPasswordAnswerAttemptCount", 0);
cmd.Parameters.AddWithValue("@failedPasswordAnswerAttemptWindowStart", createDate);
int recAdded = cmd.ExecuteNonQuery();
if (recAdded > 0)
status = MembershipCreateStatus.Success;
else
status = MembershipCreateStatus.UserRejected;
transaction.Commit();
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "CreateUser");
status = MembershipCreateStatus.ProviderError;
if (transaction != null)
transaction.Rollback();
return null;
}
}
return GetUser(username, false);
}
/// <summary>
/// Removes a user from the membership data source.
/// </summary>
/// <param name="username">The name of the user to delete.</param>
/// <param name="deleteAllRelatedData">true to delete data related to the user from the database; false to leave data related to the user in the database.</param>
/// <returns>
/// true if the user was successfully deleted; otherwise, false.
/// </returns>
public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
try
{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
conn.Open();
int userId = GetUserId(conn, username);
if (-1 == userId) return false;
// if we are supposed to delete all related data, then delegate that to those providers
if (deleteAllRelatedData)
{
MySQLRoleProvider.DeleteUserData(conn, userId);
MySQLProfileProvider.DeleteUserData(conn, userId);
}
string sql = @"DELETE {0}m
FROM my_aspnet_Users u, my_aspnet_Membership m
WHERE u.id=m.userId AND u.id=@userId";
MySqlCommand cmd = new MySqlCommand(
String.Format(sql, deleteAllRelatedData ? "u," : ""), conn);
cmd.Parameters.AddWithValue("@appId", app.FetchId(conn));
cmd.Parameters.AddWithValue("@userId", userId);
return cmd.ExecuteNonQuery() > 0;
}
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "DeleteUser");
throw new ProviderException(exceptionMessage, e);
}
}
/// <summary>
/// Gets a collection of all the users in the data source in pages of data.
/// </summary>
/// <param name="pageIndex">The index of the page of results to return. <paramref name="pageIndex"/> is zero-based.</param>
/// <param name="pageSize">The size of the page of results to return.</param>
/// <param name="totalRecords">The total number of matched users.</param>
/// <returns>
/// A <see cref="T:System.Web.Security.MembershipUserCollection"/> collection that contains a page of <paramref name="pageSize"/><see cref="T:System.Web.Security.MembershipUser"/> objects beginning at the page specified by <paramref name="pageIndex"/>.
/// </returns>
public override MembershipUserCollection GetAllUsers(int pageIndex,
int pageSize, out int totalRecords)
{
return GetUsers(null, null, pageIndex, pageSize, out totalRecords);
}
/// <summary>
/// Gets the number of users currently accessing the application.
/// </summary>
/// <returns>
/// The number of users currently accessing the application.
/// </returns>
public override int GetNumberOfUsersOnline()
{
TimeSpan onlineSpan = new TimeSpan(0, Membership.UserIsOnlineTimeWindow, 0);
DateTime compareTime = DateTime.Now.Subtract(onlineSpan);
try
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
MySqlCommand cmd = new MySqlCommand(
@"SELECT COUNT(*) FROM my_aspnet_Membership m JOIN my_aspnet_Users u
ON m.userId=u.id WHERE m.LastActivityDate > @date AND u.applicationId=@appId",
connection);
cmd.Parameters.AddWithValue("@date", compareTime);
cmd.Parameters.AddWithValue("@appId", app.FetchId(connection));
return Convert.ToInt32(cmd.ExecuteScalar());
}
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "GetNumberOfUsersOnline");
throw new ProviderException(exceptionMessage, e);
}
}
/// <summary>
/// Gets the password for the specified user name from the data source.
/// </summary>
/// <param name="username">The user to retrieve the password for.</param>
/// <param name="answer">The password answer for the user.</param>
/// <returns>
/// The password for the specified user name.
/// </returns>
public override string GetPassword(string username, string answer)
{
if (!EnablePasswordRetrieval)
throw new ProviderException(Resources.PasswordRetrievalNotEnabled);
try
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
int userId = GetUserId(connection, username);
if (-1 == userId)
throw new ProviderException("Username not found.");
string sql = @"SELECT Password, PasswordAnswer, PasswordKey, PasswordFormat,
IsLockedOut FROM my_aspnet_Membership WHERE userId=@userId";
MySqlCommand cmd = new MySqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@userId", userId);
using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
{
reader.Read();
if (reader.GetBoolean("IsLockedOut"))
throw new MembershipPasswordException(Resources.UserIsLockedOut);
string password = reader.GetString("Password");
string passwordAnswer = reader.GetValue(reader.GetOrdinal("PasswordAnswer")).ToString();
string passwordKey = reader.GetString("PasswordKey");
MembershipPasswordFormat format = (MembershipPasswordFormat)reader.GetInt32(3);
reader.Close();
if (RequiresQuestionAndAnswer &&
!(CheckPassword(answer, passwordAnswer, passwordKey, format)))
{
UpdateFailureCount(userId, "PasswordAnswer", connection);
throw new MembershipPasswordException(Resources.IncorrectPasswordAnswer);
}
if (PasswordFormat == MembershipPasswordFormat.Encrypted)
{
password = UnEncodePassword(password, format);
}
return password;
}
}
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "GetPassword");
throw new ProviderException(exceptionMessage, e);
}
}
/// <summary>
/// Gets information from the data source for a user. Provides an option to update the last-activity date/time stamp for the user.
/// </summary>
/// <param name="username">The name of the user to get information for.</param>
/// <param name="userIsOnline">true to update the last-activity date/time stamp for the user; false to return user information without updating the last-activity date/time stamp for the user.</param>
/// <returns>
/// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the specified user's information from the data source.
/// </returns>
public override MembershipUser GetUser(string username, bool userIsOnline)
{
try
{
int userId = -1;
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
userId = GetUserId(connection, username);
if (-1 == userId) return null;
}
return GetUser(userId, userIsOnline);
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "GetUser(String, Boolean)");
throw new ProviderException(exceptionMessage, e);
}
}
/// <summary>
/// Gets user information from the data source based on the unique identifier for the membership user. Provides an option to update the last-activity date/time stamp for the user.
/// </summary>
/// <param name="providerUserKey">The unique identifier for the membership user to get information for.</param>
/// <param name="userIsOnline">true to update the last-activity date/time stamp for the user; false to return user information without updating the last-activity date/time stamp for the user.</param>
/// <returns>
/// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the specified user's information from the data source.
/// </returns>
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
MySqlTransaction txn = null;
try
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
txn = connection.BeginTransaction();
MySqlCommand cmd = new MySqlCommand("", connection);
cmd.Parameters.AddWithValue("@userId", providerUserKey);
if (userIsOnline)
{
cmd.CommandText =
@"UPDATE my_aspnet_Users SET lastActivityDate = @date WHERE id=@userId";
cmd.Parameters.AddWithValue("@date", DateTime.Now);
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE my_aspnet_Membership SET LastActivityDate=@date WHERE userId=@userId";
cmd.ExecuteNonQuery();
}
cmd.CommandText = @"SELECT m.*,u.name
FROM my_aspnet_Membership m JOIN my_aspnet_Users u ON m.userId=u.id
WHERE u.id=@userId";
MembershipUser user;
using (MySqlDataReader reader = cmd.ExecuteReader())
{
if (!reader.Read()) return null;
user = GetUserFromReader(reader);
}
txn.Commit();
return user;
}
}
catch (MySqlException e)
{
if (txn != null)
txn.Rollback();
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "GetUser(Object, Boolean)");
throw new ProviderException(exceptionMessage);
}
}
/// <summary>
/// Unlocks the user.
/// </summary>
/// <param name="username">The username.</param>
/// <returns>true if the membership user was successfully unlocked;
/// otherwise, false. A value of false is also returned if the user
/// does not exist in the database. </returns>
public override bool UnlockUser(string username)
{
try
{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
conn.Open();
int userId = GetUserId(conn, username);
if (-1 == userId) return false;
string sql = @"UPDATE my_aspnet_Membership
SET IsLockedOut = false, LastLockedOutDate = @lastDate
WHERE userId=@userId";
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@lastDate", DateTime.Now);
cmd.Parameters.AddWithValue("@userId", userId);
return cmd.ExecuteNonQuery() > 0;
}
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "UnlockUser");
throw new ProviderException(exceptionMessage, e);
}
}
/// <summary>
/// Gets the user name associated with the specified e-mail address.
/// </summary>
/// <param name="email">The e-mail address to search for.</param>
/// <returns>
/// The user name associated with the specified e-mail address. If no match is found, return null.
/// </returns>
public override string GetUserNameByEmail(string email)
{
try
{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
conn.Open();
string sql = @"SELECT u.name FROM my_aspnet_Users u
JOIN my_aspnet_Membership m ON m.userid=u.id
WHERE m.Email = @email AND u.applicationId=@appId";
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@appId", app.FetchId(conn));
return (string)cmd.ExecuteScalar();
}
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "GetUserNameByEmail");
throw new ProviderException(exceptionMessage);
}
}
/// <summary>
/// Resets a user's password to a new, automatically generated password.
/// </summary>
/// <param name="username">The user to reset the password for.</param>
/// <param name="answer">The password answer for the specified user.</param>
/// <returns>The new password for the specified user.</returns>
public override string ResetPassword(string username, string answer)
{
if (!(EnablePasswordReset))
throw new NotSupportedException(Resources.PasswordResetNotEnabled);
try
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
// fetch the userid first
int userId = GetUserId(connection, username);
if (-1 == userId)
throw new ProviderException(Resources.UsernameNotFound);
if (answer == null && RequiresQuestionAndAnswer)
{
UpdateFailureCount(userId, "PasswordAnswer", connection);
throw new ProviderException(Resources.PasswordRequiredForReset);
}
string newPassword = Membership.GeneratePassword(newPasswordLength, MinRequiredNonAlphanumericCharacters);
ValidatePasswordEventArgs Args = new ValidatePasswordEventArgs(username, newPassword, true);
OnValidatingPassword(Args);
if (Args.Cancel)
{
if (!(Args.FailureInformation == null))
throw Args.FailureInformation;
else
throw new MembershipPasswordException(Resources.PasswordResetCanceledNotValid);
}
MySqlCommand cmd = new MySqlCommand(@"SELECT PasswordAnswer,
PasswordKey, PasswordFormat, IsLockedOut
FROM my_aspnet_Membership WHERE userId=@userId", connection);
cmd.Parameters.AddWithValue("@userId", userId);
string passwordKey = String.Empty;
MembershipPasswordFormat format;
using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
{
reader.Read();
if (reader.GetBoolean("IsLockedOut"))
throw new MembershipPasswordException(Resources.UserIsLockedOut);
object passwordAnswer = reader.GetValue(reader.GetOrdinal("PasswordAnswer"));
passwordKey = reader.GetString("PasswordKey");
format = (MembershipPasswordFormat)reader.GetByte("PasswordFormat");
reader.Close();
if (RequiresQuestionAndAnswer)
{
if (!CheckPassword(answer, (string)passwordAnswer, passwordKey, format))
{
UpdateFailureCount(userId, "PasswordAnswer", connection);
throw new MembershipPasswordException(Resources.IncorrectPasswordAnswer);
}
}
}
cmd.CommandText = @"UPDATE my_aspnet_Membership
SET Password = @pass, LastPasswordChangedDate = @lastPassChange
WHERE userId=@userId";
cmd.Parameters.AddWithValue("@pass",
EncodePassword(newPassword, passwordKey, format));
cmd.Parameters.AddWithValue("@lastPassChange", DateTime.Now);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected != 1)
throw new MembershipPasswordException(Resources.ErrorResettingPassword);
return newPassword;
}
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "ResetPassword");
throw new ProviderException(exceptionMessage, e);
}
}
/// <summary>
/// Updates information about a user in the data source.
/// </summary>
/// <param name="user">A <see cref="T:System.Web.Security.MembershipUser"/> object
/// that represents the user to update and the updated information for the user.</param>
public override void UpdateUser(MembershipUser user)
{
try
{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
conn.Open();
int userId = GetUserId(conn, user.UserName);
if (-1 == userId)
throw new ProviderException(Resources.UsernameNotFound);
string sql = @"UPDATE my_aspnet_Membership m, my_aspnet_Users u
SET m.Email=@email, m.Comment=@comment, m.IsApproved=@isApproved,
m.LastLoginDate=@lastLoginDate, u.lastActivityDate=@lastActivityDate,
m.LastActivityDate=@lastActivityDate
WHERE m.userId=u.id AND u.name LIKE @name AND u.applicationId=@appId";
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Email", user.Email);
cmd.Parameters.AddWithValue("@Comment", user.Comment);
cmd.Parameters.AddWithValue("@isApproved", user.IsApproved);
cmd.Parameters.AddWithValue("@lastLoginDate", user.LastLoginDate);
cmd.Parameters.AddWithValue("@lastActivityDate", user.LastActivityDate);
cmd.Parameters.AddWithValue("@name", user.UserName);
cmd.Parameters.AddWithValue("@appId", app.FetchId(conn));
cmd.ExecuteNonQuery();
}
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "UpdateUser");
throw new ProviderException(exceptionMessage);
}
}
/// <summary>
/// Verifies that the specified user name and password exist in the data source.
/// </summary>
/// <param name="username">The name of the user to validate.</param>
/// <param name="password">The password for the specified user.</param>
/// <returns>
/// true if the specified username and password are valid; otherwise, false.
/// </returns>
public override bool ValidateUser(string username, string password)
{
bool isValid = false;
try
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
// first get the user id. If that is -1, then the user doesn't exist
// so we just return false since we can't bump any counters
int userId = GetUserId(connection, username);
if (-1 == userId) return false;
string sql = @"SELECT Password, PasswordKey, PasswordFormat, IsApproved,
Islockedout FROM my_aspnet_Membership WHERE userId=@userId";
MySqlCommand cmd = new MySqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@userId", userId);
using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
{
if (!reader.HasRows) return false;
reader.Read();
if (reader.GetBoolean("IsLockedOut")) return false;
string pwd = reader.GetString(0);
string passwordKey = reader.GetString(1);
MembershipPasswordFormat format = (MembershipPasswordFormat)
reader.GetInt32(2);
bool isApproved = reader.GetBoolean(3);
reader.Close();
if (!CheckPassword(password, pwd, passwordKey, format))
UpdateFailureCount(userId, "Password", connection);
else if (isApproved)
{
isValid = true;
DateTime currentDate = DateTime.Now;
MySqlCommand updateCmd = new MySqlCommand(
@"UPDATE my_aspnet_Membership m, my_aspnet_Users u
SET m.LastLoginDate = @lastLoginDate, u.lastActivityDate = @date,
m.LastActivityDate=@date
WHERE m.userId=@userid AND u.id=@userid", connection);
updateCmd.Parameters.AddWithValue("@lastLoginDate", currentDate);
updateCmd.Parameters.AddWithValue("@date", currentDate);
updateCmd.Parameters.AddWithValue("@userid", userId);
updateCmd.ExecuteNonQuery();
}
}
return isValid;
}
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "ValidateUser");
throw new ProviderException(exceptionMessage, e);
}
}
/// <summary>
/// Gets a collection of membership users where the user name contains the specified user name to match.
/// </summary>
/// <param name="usernameToMatch">The user name to search for.</param>
/// <param name="pageIndex">The index of the page of results to return. <paramref name="pageIndex"/> is zero-based.</param>
/// <param name="pageSize">The size of the page of results to return.</param>
/// <param name="totalRecords">The total number of matched users.</param>
/// <returns>
/// A <see cref="T:System.Web.Security.MembershipUserCollection"/> collection that contains a page of <paramref name="pageSize"/><see cref="T:System.Web.Security.MembershipUser"/> objects beginning at the page specified by <paramref name="pageIndex"/>.
/// </returns>
public override MembershipUserCollection FindUsersByName(string usernameToMatch,
int pageIndex, int pageSize, out int totalRecords)
{
return GetUsers(usernameToMatch, null, pageIndex, pageSize, out totalRecords);
}
/// <summary>
/// Gets a collection of membership users where the e-mail address contains the specified e-mail address to match.
/// </summary>
/// <param name="emailToMatch">The e-mail address to search for.</param>
/// <param name="pageIndex">The index of the page of results to return. <paramref name="pageIndex"/> is zero-based.</param>
/// <param name="pageSize">The size of the page of results to return.</param>
/// <param name="totalRecords">The total number of matched users.</param>
/// <returns>
/// A <see cref="T:System.Web.Security.MembershipUserCollection"/> collection that contains a page of <paramref name="pageSize"/><see cref="T:System.Web.Security.MembershipUser"/> objects beginning at the page specified by <paramref name="pageIndex"/>.
/// </returns>
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex,
int pageSize, out int totalRecords)
{
return GetUsers(null, emailToMatch, pageIndex, pageSize, out totalRecords);
}
#endregion
#region Private Methods
private int GetUserId(MySqlConnection connection, string username)
{
MySqlCommand cmd = new MySqlCommand(
"SELECT id FROM my_aspnet_Users WHERE name = @name AND applicationId=@appId", connection);
cmd.Parameters.AddWithValue("@name", username);
cmd.Parameters.AddWithValue("@appId", app.FetchId(connection));
object id = cmd.ExecuteScalar();
if (id == null) return -1;
return (int)id;
}
private void WriteToEventLog(Exception e, string action)
{
using (EventLog log = new EventLog())
{
log.Source = eventSource;
log.Log = eventLog;
string message = "An exception occurred communicating with the data source." +
Environment.NewLine + Environment.NewLine;
message += "Action: " + action + Environment.NewLine + Environment.NewLine;
message += "Exception: " + e;
log.WriteEntry(message);
}
}
private MembershipUser GetUserFromReader(MySqlDataReader reader)
{
object providerUserKey = reader.GetInt32("userId");
string username = reader.GetString("name");
string email = null;
if (!reader.IsDBNull(reader.GetOrdinal("Email")))
email = reader.GetString("Email");
string passwordQuestion = "";
if (!(reader.GetValue(reader.GetOrdinal("PasswordQuestion")) == DBNull.Value))
passwordQuestion = reader.GetString("PasswordQuestion");
string comment = "";
if (!(reader.GetValue(reader.GetOrdinal("Comment")) == DBNull.Value))
comment = reader.GetString("Comment");
bool isApproved = reader.GetBoolean("IsApproved");
bool isLockedOut = reader.GetBoolean("IsLockedOut");
DateTime creationDate = reader.GetDateTime("CreationDate");
DateTime lastLoginDate = new DateTime();
if (!(reader.GetValue(reader.GetOrdinal("LastLoginDate")) == DBNull.Value))
lastLoginDate = reader.GetDateTime("LastLoginDate");
DateTime lastActivityDate = reader.GetDateTime("LastActivityDate");
DateTime lastPasswordChangedDate = reader.GetDateTime("LastPasswordChangedDate");
DateTime lastLockedOutDate = new DateTime();
if (!(reader.GetValue(reader.GetOrdinal("LastLockedoutDate")) == DBNull.Value))
lastLockedOutDate = reader.GetDateTime("LastLockedoutDate");
MembershipUser u =
new MembershipUser(Name, username, providerUserKey, email, passwordQuestion, comment, isApproved,
isLockedOut, creationDate, lastLoginDate, lastActivityDate, lastPasswordChangedDate,
lastLockedOutDate);
return u;
}
private string UnEncodePassword(string encodedPassword, MembershipPasswordFormat format)
{
string password = encodedPassword;
if (format == MembershipPasswordFormat.Clear)
return encodedPassword;
else if (format == MembershipPasswordFormat.Encrypted)
return Encoding.Unicode.GetString(DecryptPassword(
Convert.FromBase64String(password)));
else if (format == MembershipPasswordFormat.Hashed)
throw new ProviderException(Resources.CannotUnencodeHashedPwd);
else
throw new ProviderException(Resources.UnsupportedPasswordFormat);
}
private string GetPasswordKey()
{
RNGCryptoServiceProvider cryptoProvider =
new RNGCryptoServiceProvider();
byte[] key = new byte[16];
cryptoProvider.GetBytes(key);
return Convert.ToBase64String(key);
}
/// <summary>
/// this method is only necessary because early versions of Mono did not support
/// the HashAlgorithmType property
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
private string HashPasswordBytes(byte[] key, byte[] bytes)
{
HashAlgorithm hash = HashAlgorithm.Create(Membership.HashAlgorithmType);
if (hash is KeyedHashAlgorithm)
{
KeyedHashAlgorithm keyedHash = hash as KeyedHashAlgorithm;
keyedHash.Key = key;
}
return Convert.ToBase64String(hash.ComputeHash(bytes));
}
private string EncodePassword(string password, string passwordKey,
MembershipPasswordFormat format)
{
if (password == null)
return null;
if (format == MembershipPasswordFormat.Clear)
return password;
byte[] passwordBytes = Encoding.Unicode.GetBytes(password);
byte[] keyBytes = Convert.FromBase64String(passwordKey);
byte[] keyedBytes = new byte[passwordBytes.Length + keyBytes.Length];
Array.Copy(keyBytes, keyedBytes, keyBytes.Length);
Array.Copy(passwordBytes, 0, keyedBytes, keyBytes.Length, passwordBytes.Length);
if (format == MembershipPasswordFormat.Encrypted)
{
byte[] encryptedBytes = EncryptPassword(passwordBytes);
return Convert.ToBase64String(encryptedBytes);
}
else if (format == MembershipPasswordFormat.Hashed)
return HashPasswordBytes(keyBytes, keyedBytes);
else
throw new ProviderException(Resources.UnsupportedPasswordFormat);
}
private void UpdateFailureCount(int userId, string failureType, MySqlConnection connection)
{
MySqlCommand cmd = new MySqlCommand(
@"SELECT FailedPasswordAttemptCount,
FailedPasswordAttemptWindowStart, FailedPasswordAnswerAttemptCount,
FailedPasswordAnswerAttemptWindowStart FROM my_aspnet_Membership
WHERE userId=@userId", connection);
cmd.Parameters.AddWithValue("@userId", userId);
DateTime windowStart = new DateTime();
int failureCount = 0;
try
{
using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
{
if (!reader.HasRows)
throw new ProviderException(Resources.UnableToUpdateFailureCount);
reader.Read();
if (failureType == "Password")
{
failureCount = reader.GetInt32(0);
windowStart = reader.GetDateTime(1);
}
if (failureType == "PasswordAnswer")
{
failureCount = reader.GetInt32(2);
windowStart = reader.GetDateTime(3);
}
}
DateTime windowEnd = windowStart.AddMinutes(PasswordAttemptWindow);
if (failureCount == 0 || DateTime.Now > windowEnd)
{
if (failureType == "Password")
{
cmd.CommandText =
@"UPDATE my_aspnet_Membership
SET FailedPasswordAttemptCount = @count,
FailedPasswordAttemptWindowStart = @windowStart
WHERE userId=@userId";
}
if (failureType == "PasswordAnswer")
{
cmd.CommandText =
@"UPDATE my_aspnet_Membership
SET FailedPasswordAnswerAttemptCount = @count,
FailedPasswordAnswerAttemptWindowStart = @windowStart
WHERE userId = @userId";
}
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@count", 1);
cmd.Parameters.AddWithValue("@windowStart", DateTime.Now);
cmd.Parameters.AddWithValue("@userId", userId);
if (cmd.ExecuteNonQuery() < 0)
throw new ProviderException(Resources.UnableToUpdateFailureCount);
}
else
{
failureCount += 1;
if (failureCount >= MaxInvalidPasswordAttempts)
{
cmd.CommandText =
@"UPDATE my_aspnet_Membership SET IsLockedOut = @isLockedOut,
LastLockedOutDate = @lastLockedOutDate WHERE userId=@userId";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@isLockedOut", true);
cmd.Parameters.AddWithValue("@lastLockedOutDate", DateTime.Now);
cmd.Parameters.AddWithValue("@userId", userId);
if (cmd.ExecuteNonQuery() < 0)
throw new ProviderException(Resources.UnableToLockOutUser);
}
else
{
if (failureType == "Password")
{
cmd.CommandText =
@"UPDATE my_aspnet_Membership
SET FailedPasswordAttemptCount = @count WHERE userId=@userId";
}
if (failureType == "PasswordAnswer")
{
cmd.CommandText =
@"UPDATE my_aspnet_Membership
SET FailedPasswordAnswerAttemptCount = @count
WHERE userId=@userId";
}
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@count", failureCount);
cmd.Parameters.AddWithValue("@userId", userId);
if (cmd.ExecuteNonQuery() < 0)
throw new ProviderException("Unable to update failure count.");
}
}
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "UpdateFailureCount");
throw new ProviderException(exceptionMessage, e);
}
}
private bool CheckPassword(string password, string dbpassword,
string passwordKey, MembershipPasswordFormat format)
{
password = EncodePassword(password, passwordKey, format);
return password == dbpassword;
}
private void GetPasswordInfo(MySqlConnection connection, int userId,
out string passwordKey, out MembershipPasswordFormat passwordFormat)
{
MySqlCommand cmd = new MySqlCommand(
@"SELECT PasswordKey, PasswordFormat FROM my_aspnet_Membership WHERE
userId=@userId", connection);
cmd.Parameters.AddWithValue("@userId", userId);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
passwordKey = reader.GetString(reader.GetOrdinal("PasswordKey"));
passwordFormat = (MembershipPasswordFormat)reader.GetByte(
reader.GetOrdinal("PasswordFormat"));
}
}
private MembershipUserCollection GetUsers(string username, string email,
int pageIndex, int pageSize, out int totalRecords)
{
MembershipUserCollection users = new MembershipUserCollection();
try
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = connection;
string sql = @"SELECT SQL_CALC_FOUND_ROWS u.name,m.* FROM my_aspnet_Users u
JOIN my_aspnet_Membership m ON m.userId=u.id
WHERE u.applicationId=@appId";
if (username != null)
{
sql += " AND u.name LIKE @name";
cmd.Parameters.AddWithValue("@name", username);
}
else if (email != null)
{
sql += " AND m.Email LIKE @email";
cmd.Parameters.AddWithValue("@email", email);
}
sql += " ORDER BY u.id ASC LIMIT {0},{1}";
cmd.CommandText = String.Format(sql, pageIndex * pageSize, pageSize);
cmd.Parameters.AddWithValue("@appId", app.FetchId(connection));
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
users.Add(GetUserFromReader(reader));
}
cmd.CommandText = "SELECT FOUND_ROWS()";
cmd.Parameters.Clear();
totalRecords = Convert.ToInt32(cmd.ExecuteScalar());
}
return users;
}
catch (MySqlException e)
{
if (WriteExceptionsToEventLog)
WriteToEventLog(e, "GetUsers");
throw new ProviderException(exceptionMessage);
}
}
private void ValidateQA(string question, string answer)
{
if (RequiresQuestionAndAnswer && String.IsNullOrEmpty(question))
throw new ArgumentException(Resources.PasswordQuestionInvalid);
if (RequiresQuestionAndAnswer && String.IsNullOrEmpty(answer))
throw new ArgumentException(Resources.PasswordAnswerInvalid);
}
private bool ValidatePassword(string password, string argumentName, bool throwExceptions)
{
string exceptionString = null;
object correctValue = MinRequiredPasswordLength;
if (password.Length < MinRequiredPasswordLength)
exceptionString = Resources.PasswordNotLongEnough;
else
{
int count = 0;
foreach (char c in password)
if (!char.IsLetterOrDigit(c))
count++;
if (count < MinRequiredNonAlphanumericCharacters)
exceptionString = Resources.NotEnoughNonAlphaNumericInPwd;
correctValue = MinRequiredNonAlphanumericCharacters;
}
if (exceptionString != null)
{
if (throwExceptions)
throw new ArgumentException(
string.Format(exceptionString, argumentName, correctValue),
argumentName);
else
return false;
}
if (PasswordStrengthRegularExpression.Length > 0)
if (!Regex.IsMatch(password, PasswordStrengthRegularExpression))
return false;
return true;
}
#endregion
}
}
|