Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class UserProvider : MembershipProvider
- {
- private int maxInvalidPasswordAttempts;
- private int passwordAttemptWindow;
- private int minRequiredNonalphanumericCharacters;
- private int minRequiredPasswordLength;
- private bool enablePasswordReset;
- private string passwordStrengthRegularExpression;
- private int minRequiredNonAlphanumericCharacters;
- private bool enablePasswordRetrieval;
- private bool requiresQuestionAndAnswer;
- private MembershipPasswordFormat passwordFormat;
- private bool requiresUniqueEmail;
- public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
- {
- if (config == null)
- throw new ArgumentNullException("config");
- ApplicationName = GetConfigValue(config["applicationName"], System.Web.Hosting.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"], "6"));
- enablePasswordReset = Convert.ToBoolean(GetConfigValue(config["enablePasswordReset"], "true"));
- passwordStrengthRegularExpression = Convert.ToString(GetConfigValue(config["passwordStrengthRegularExpression"], ""));
- base.Initialize(name, config);
- }
- #region Overrides of MembershipProvider
- #region Methods
- /// <summary>
- /// Adds a new membership user to the data source.
- /// </summary>
- /// <returns>
- /// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the information for the newly created user.
- /// </returns>
- /// <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>
- public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
- {
- return CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, Gender.Unknown, out status);
- }
- public User CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, Gender gender, out MembershipCreateStatus status)
- {
- var args = new ValidatePasswordEventArgs(username, password, true);
- OnValidatingPassword(args);
- if (args.Cancel)
- {
- status = MembershipCreateStatus.InvalidPassword;
- return null;
- }
- if (requiresUniqueEmail && GetUserNameByEmail(email) != "")
- {
- status = MembershipCreateStatus.DuplicateEmail;
- return null;
- }
- var user = new User(username, gender, password, email, passwordQuestion, isApproved, false);
- using (var db = new DataContext())
- {
- try
- {
- db.Users.Add(user);
- status = db.SaveChanges() > 0
- ? MembershipCreateStatus.Success
- : MembershipCreateStatus.UserRejected;
- return user;
- }
- catch
- {
- status = MembershipCreateStatus.ProviderError;
- }
- return null;
- }
- }
- /// <summary>
- /// Processes a request to update the password question and answer for a membership user.
- /// </summary>
- /// <returns>
- /// true if the password question and answer are updated successfully; otherwise, false.
- /// </returns>
- /// <param name="username">The user to change the password question and answer for. </param><param name="password">The password for the specified user. </param><param name="newPasswordQuestion">The new password question for the specified user. </param><param name="newPasswordAnswer">The new password answer for the specified user. </param>
- public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// Gets the password for the specified user name from the data source.
- /// </summary>
- /// <returns>
- /// The password for the specified user name.
- /// </returns>
- /// <param name="username">The user to retrieve the password for. </param><param name="answer">The password answer for the user. </param>
- public override string GetPassword(string username, string answer)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// Processes a request to update the password for a membership user.
- /// </summary>
- /// <returns>
- /// true if the password was updated successfully; otherwise, false.
- /// </returns>
- /// <param name="username">The user to update the password for. </param><param name="oldPassword">The current password for the specified user. </param><param name="newPassword">The new password for the specified user. </param>
- public override bool ChangePassword(string username, string oldPassword, string newPassword)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// Resets a user's password to a new, automatically generated password.
- /// </summary>
- /// <returns>
- /// The new password for the specified user.
- /// </returns>
- /// <param name="username">The user to reset the password for. </param><param name="answer">The password answer for the specified user. </param>
- public override string ResetPassword(string username, string answer)
- {
- throw new NotImplementedException();
- }
- /// <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)
- {
- using (var db = new DataContext())
- {
- var userDB = (from u in db.Users
- where u.ProviderUserKey == user.ProviderUserKey
- select u).SingleOrDefault();
- userDB.IsApproved = user.IsApproved;
- userDB.LastLoginDate = user.LastLoginDate;
- db.SaveChanges();
- }
- }
- /// <summary>
- /// Verifies that the specified user name and password exist in the data source.
- /// </summary>
- /// <returns>
- /// true if the specified username and password are valid; otherwise, false.
- /// </returns>
- /// <param name="email">The email of the user to validate. </param>
- /// <param name="password">The password for the specified user. </param>
- public override bool ValidateUser(string email, string password)
- {
- using (var db = new DataContext())
- {
- var user = (from u in db.Users
- where u.Email == email && u.Password == password
- select u).SingleOrDefault();
- return user != null && (user.IsApproved && !user.IsLockedOut);
- }
- }
- /// <summary>
- /// Clears a lock so that the membership user can be validated.
- /// </summary>
- /// <returns>
- /// true if the membership user was successfully unlocked; otherwise, false.
- /// </returns>
- /// <param name="userName">The membership user whose lock status you want to clear.</param>
- public override bool UnlockUser(string userName)
- {
- throw new NotImplementedException();
- }
- /// <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>
- /// <returns>
- /// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the specified user's information from the data source.
- /// </returns>
- /// <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>
- public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
- {
- return GetUser(providerUserKey);
- }
- public User GetUser(object providerUserKey)
- {
- using (var db = new DataContext())
- {
- var user = (from u in db.Users
- where u.ProviderUserKey == providerUserKey
- select u).SingleOrDefault();
- return user;
- }
- }
- /// <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>
- /// <returns>
- /// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the specified user's information from the data source.
- /// </returns>
- /// <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>
- public override MembershipUser GetUser(string username, bool userIsOnline)
- {
- using (var db = new DataContext())
- {
- var user = (from u in db.Users
- where u.UserName == username
- select u).SingleOrDefault();
- return user;
- }
- }
- /// <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>
- /// <returns>
- /// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the specified user's information from the data source.
- /// </returns>
- /// <param name="email">The email of the user to get information for. </param>
- public User GetUser(string email)
- {
- using (var db = new DataContext())
- {
- var user = (from u in db.Users
- where u.Email == email
- select u).SingleOrDefault();
- return user;
- }
- }
- /// <summary>
- /// Gets the user name associated with the specified e-mail address.
- /// </summary>
- /// <returns>
- /// The user name associated with the specified e-mail address. If no match is found, return null.
- /// </returns>
- /// <param name="email">The e-mail address to search for. </param>
- public override string GetUserNameByEmail(string email)
- {
- using (var db = new DataContext())
- {
- return (from u in db.Users
- where u.Email == email
- select u.UserName).SingleOrDefault();
- }
- }
- /// <summary>
- /// Removes a user from the membership data source.
- /// </summary>
- /// <returns>
- /// true if the user was successfully deleted; otherwise, false.
- /// </returns>
- /// <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>
- public override bool DeleteUser(string username, bool deleteAllRelatedData)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// Gets a collection of all the users in the data source in pages of data.
- /// </summary>
- /// <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>
- /// <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>
- public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
- {
- using (var db = new DataContext())
- {
- var users = from u in db.Users
- select u;
- var returnList = new MembershipUserCollection();
- foreach (var user in users)
- returnList.Add(user);
- totalRecords = returnList.Count;
- return returnList;
- }
- }
- /// <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()
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// Gets a collection of membership users where the user name contains the specified user name to match.
- /// </summary>
- /// <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>
- /// <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>
- public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
- {
- using (var db = new DataContext())
- {
- var users = (from u in db.Users
- where u.UserName.Contains(usernameToMatch)
- select u).Skip(pageIndex).Take(pageSize);
- var returnList = new MembershipUserCollection();
- foreach (var user in users)
- returnList.Add(user);
- totalRecords = returnList.Count;
- return returnList;
- }
- }
- /// <summary>
- /// Gets a collection of membership users where the e-mail address contains the specified e-mail address to match.
- /// </summary>
- /// <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>
- /// <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>
- public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
- {
- using (var db = new DataContext())
- {
- var users = (from u in db.Users
- where u.Email.Contains(emailToMatch)
- select u).Skip(pageIndex).Take(pageSize).ToList();
- var msc = new MembershipUserCollection();
- foreach (var user in users)
- msc.Add(user);
- totalRecords = users.Count;
- return msc;
- }
- }
- #endregion
- #region Properties
- /// <summary>
- /// Indicates whether the membership provider is configured to allow users to retrieve their passwords.
- /// </summary>
- /// <returns>
- /// true if the membership provider is configured to support password retrieval; otherwise, false. The default is false.
- /// </returns>
- public override bool EnablePasswordRetrieval
- {
- get { return enablePasswordRetrieval; }
- }
- /// <summary>
- /// Indicates whether the membership provider is configured to allow users to reset their passwords.
- /// </summary>
- /// <returns>
- /// true if the membership provider supports password reset; otherwise, false. The default is true.
- /// </returns>
- public override bool EnablePasswordReset
- {
- get { return enablePasswordReset; }
- }
- /// <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>
- /// <returns>
- /// true if a password answer is required for password reset and retrieval; otherwise, false. The default is true.
- /// </returns>
- public override bool RequiresQuestionAndAnswer
- {
- get { return requiresQuestionAndAnswer; }
- }
- /// <summary>
- /// The name of the application using the custom membership provider.
- /// </summary>
- /// <returns>
- /// The name of the application using the custom membership provider.
- /// </returns>
- public override string ApplicationName { get; set; }
- /// <summary>
- /// Gets the number of invalid password or password-answer attempts allowed before the membership user is locked out.
- /// </summary>
- /// <returns>
- /// The number of invalid password or password-answer attempts allowed before the membership user is locked out.
- /// </returns>
- 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>
- /// <returns>
- /// 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.
- /// </returns>
- public override int PasswordAttemptWindow
- {
- get { return passwordAttemptWindow; }
- }
- /// <summary>
- /// Gets a value indicating whether the membership provider is configured to require a unique e-mail address for each user name.
- /// </summary>
- /// <returns>
- /// true if the membership provider requires a unique e-mail address; otherwise, false. The default is true.
- /// </returns>
- public override bool RequiresUniqueEmail
- {
- get { return requiresUniqueEmail; }
- }
- /// <summary>
- /// Gets a value indicating the format for storing passwords in the membership data store.
- /// </summary>
- /// <returns>
- /// One of the <see cref="T:System.Web.Security.MembershipPasswordFormat"/> values indicating the format for storing passwords in the data store.
- /// </returns>
- public override MembershipPasswordFormat PasswordFormat
- {
- get { return passwordFormat; }
- }
- /// <summary>
- /// Gets the minimum length required for a password.
- /// </summary>
- /// <returns>
- /// The minimum length required for a password.
- /// </returns>
- public override int MinRequiredPasswordLength
- {
- get { return minRequiredPasswordLength; }
- }
- /// <summary>
- /// Gets the minimum number of special characters that must be present in a valid password.
- /// </summary>
- /// <returns>
- /// The minimum number of special characters that must be present in a valid password.
- /// </returns>
- public override int MinRequiredNonAlphanumericCharacters
- {
- get { return minRequiredNonAlphanumericCharacters; }
- }
- /// <summary>
- /// Gets the regular expression used to evaluate a password.
- /// </summary>
- /// <returns>
- /// A regular expression used to evaluate a password.
- /// </returns>
- public override string PasswordStrengthRegularExpression
- {
- get { return passwordStrengthRegularExpression; }
- }
- #endregion
- #endregion
- private string GetConfigValue(string configValue, string defaultValue)
- {
- return string.IsNullOrEmpty(configValue) ? defaultValue : configValue;
- }
- }
Add Comment
Please, Sign In to add comment