/// <summary> /// Gets all the users in the database /// </summary> /// <param name="pageIndex">Current page index</param> /// <param name="pageSize">Number of results per page</param> /// <param name="totalRecords">Total number of users returned</param> /// <returns>MembershpUserCollection object with a list of users on the page</returns> public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) { var users = new MembershipUserCollection(); totalRecords = 0; try { int start = pageSize * pageIndex; totalRecords = GetMemberCount(); Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); List <MemberInfo> mlist = new List <MemberInfo>(dal.GetMembers(pageIndex * pageSize, pageSize, null, null)); foreach (MemberInfo m in mlist) { SnitzMembershipUser mu = GetUserFromMember(m); users.Add(mu); } } catch { } return(users); }
/// <summary> /// Gets a MembershipUser object with a given key /// </summary> public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) { SnitzMembershipUser u = null; try { Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); MemberInfo m = dal.GetById(Convert.ToInt32(providerUserKey)); if (userIsOnline) { m.LastUpdateDate = DateTime.UtcNow; dal.UpdateVisit(m); } if (m != null) { u = GetUserFromMember(m); } } catch { } return(u); }
/// <summary> /// Gets a MembershipUser object with a given username /// </summary> public override MembershipUser GetUser(string username, bool userIsOnline) { SnitzMembershipUser u = null; try { Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); MemberInfo m = dal.GetByName(username).SingleOrDefault(); if (userIsOnline) { m.LastUpdateDate = DateTime.UtcNow; dal.UpdateVisit(m); } if (m != null) { u = GetUserFromMember(m); } } catch { } return(u); }
/// <summary> /// Change the password retreival/reset question and answer pair /// </summary> /// <param name="username">Username the question and answer are being changed for</param> /// <param name="password">Current password</param> /// <param name="newPasswordQuestion">New password question</param> /// <param name="newPasswordAnswer">New password answer (will also be encrypted)</param> /// <returns>True if successful. Defaults to false.</returns> public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) { if (newPasswordAnswer != "validationcode") { if (!ValidateUser(username, password)) { return(false); } } bool ret; try { Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); MemberInfo m = dal.GetByName(username).SingleOrDefault(); m.ValidationKey = newPasswordQuestion; m.PasswordChangeKey = newPasswordAnswer; dal.Update(m); ret = true; } catch { ret = false; } return(ret); }
/// <summary> /// Permanently deletes a user from the database /// </summary> /// <param name="username">Username to delete</param> /// <param name="deleteAllRelatedData">Should or shouldn't delete related user data</param> /// <returns>True if successful. Defaults to false.</returns> public override bool DeleteUser(string username, bool deleteAllRelatedData) { bool ret; try { Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); MemberInfo m = dal.GetByName(username).SingleOrDefault(); dal.Delete(m); IRoles roleDal = Factory <IRoles> .Create("Role"); roleDal.RemoveUsersFromRoles(new[] { username }, roleDal.GetRolesForUser(username)); if (deleteAllRelatedData) { IMember membershipDal = Factory <IMember> .Create("Member"); membershipDal.DeleteProfile(m); } // Nothing was thrown, so go ahead and return true ret = true; } catch { // Couldn't find the user or was not able to delete ret = false; } return(ret); }
/// <summary> /// Resets the passwords with a generated value /// </summary> /// <param name="username">User the password is being reset for</param> /// <param name="answer">Password retrieval answer</param> /// <returns>Newly generated password</returns> public override string ResetPassword(string username, string answer) { // Default password is empty string pass = String.Empty; try { Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); MemberInfo m = dal.GetByName(username).SingleOrDefault(); // We found a user by that name if (m != null) { // Check if the returned password answer matches if (_requiresQuestionAndAnswer) { if (EncodePassword(answer) == m.ValidationKey) { // Create a new password with the minimum number of characters pass = GeneratePassword(MinRequiredPasswordLength); // If the password format is hashed, there must be a salt added if (PasswordFormat == MembershipPasswordFormat.Hashed) { //string salt = GenerateSalt(); //pass = pass + salt; m.Password = SHA256Hash(pass); dal.Update(m); } //m.Password = EncodePassword(pass); //m.PasswordSalt = salt; // Reset everyting //ResetAuthenticationFailures(ref m, DateTime.UtcNow); } } else { if (PasswordFormat == MembershipPasswordFormat.Hashed) { //string salt = GenerateSalt(); //pass = pass + salt; pass = GeneratePassword(MinRequiredPasswordLength); m.Password = SHA256Hash(pass); dal.Update(m); } } } } catch { } return(pass); }
/************************************************************************* * User information retreival methods *************************************************************************/ /// <summary> /// Gets the username by a given matching email address /// </summary> public override string GetUserNameByEmail(string email) { string username = String.Empty; try { Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); MemberInfo m = dal.GetByEmail(email); } catch { } return(username); }
/// <summary> /// Updates an existing user with new settings /// </summary> /// <param name="user">MembershipUser object to modify</param> public override void UpdateUser(MembershipUser user) { Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); MemberInfo m = dal.GetByName(user.UserName).SingleOrDefault(); if (m != null) { m.Email = user.Email; m.IsValid = user.IsApproved; m.LastVisitDate = DateTime.UtcNow; } //todo: dal.UpdateVisit(m); }
/// <summary> /// Change the current password for a new one. Note: Both are required. /// </summary> /// <param name="username">Username the password is being changed for</param> /// <param name="oldPassword">Old password to verify owner</param> /// <param name="newPassword">New password</param> /// <returns>True if successful. Defaults to false.</returns> public override bool ChangePassword(string username, string oldPassword, string newPassword) { if (!ValidateUser(username, oldPassword)) { return(false); } var args = new ValidatePasswordEventArgs(username, newPassword, false); OnValidatingPassword(args); if (args.Cancel) { if (args.FailureInformation != null) { throw args.FailureInformation; } else { throw new MembershipPasswordException("Password change has been cancelled due to a validation failure."); } } bool ret; try { Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); MemberInfo m = dal.GetByName(username).SingleOrDefault(); if (PasswordFormat == MembershipPasswordFormat.Hashed) { //string salt = GenerateSalt(); //newPassword = newPassword + salt; m.Password = SHA256Hash(newPassword); } dal.Update(m); ret = true; } catch { ret = false; } return(ret); }
/************************************************************************* * User authentication methods *************************************************************************/ /// <summary> /// Authenticates a user with the given username and password /// </summary> /// <param name="password">The login username</param> /// <param name="username">Login password</param> /// <returns>True if successful. Defaults to false.</returns> public override bool ValidateUser(string username, string password) { // Return status defaults to false. bool ret = false; try { Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); MemberInfo m = dal.GetByName(username).SingleOrDefault(); // We found a user by the username if (m != null) { // A user cannot login if not approved or locked out if ((!m.IsValid) || m.Status == 0) { } else { // Trigger period DateTime dt = DateTime.UtcNow; // Check the given password and the one stored (and salt, if it exists) if (CheckPassword(password, m.Password, "")) { m.LastVisitDate = dt; m.LastUpdateDate = dt; // Save changes dal.UpdateVisit(m); // Reset past failures //ResetAuthenticationFailures(ref m, dt); ret = true; } } } } catch (Exception) { // Nothing was thrown, so go ahead and return true ret = false; } return(ret); }
public MembershipUserCollection FindUsersByInitial(string initial, int pageIndex, int pageSize, out int totalRecords) { var users = new MembershipUserCollection(); totalRecords = 0; try { int start = pageSize * pageIndex; Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); List <MemberInfo> mlist = new List <MemberInfo>(dal.GetByName(initial + "%")); foreach (MemberInfo m in mlist) { users.Add(GetUserFromMember(m)); } } catch { } return(users); }
/// <summary> /// Unlocks a user (after too many login attempts perhaps) /// </summary> /// <param name="userName">Username to unlock</param> /// <returns>True if successful. Defaults to false.</returns> public override bool UnlockUser(string userName) { // Return status defaults to false bool ret; try { Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); MemberInfo m = dal.GetByName(userName).SingleOrDefault(); m.Status = 1; dal.Update(m); // A user was found and nothing was thrown ret = true; } catch { // Couldn't find the user or there was an error ret = false; } return(ret); }
/************************************************************************* * User related methods : create, update, unlock, delete methods. *************************************************************************/ /// <summary> /// Creates a new user with a given set of default values /// </summary> public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, 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); } // If no user with this name already exists MembershipUser mu = GetUser(username, false); if (mu == null || mu.UserName.ToLower() == "guest") { DateTime createdDate = DateTime.UtcNow; //if (PasswordFormat == MembershipPasswordFormat.Hashed) //{ // string salt = GenerateSalt(); // password = password + salt; //} var m = new MemberInfo { Username = username, Password = EncodePassword(password), Email = email }; // Set the password retrieval question and answer if they are required if (RequiresQuestionAndAnswer) { m.PasswordChangeKey = passwordQuestion; m.ValidationKey = EncodePassword(passwordAnswer); } m.IsValid = isApproved; m.Status = 0; m.ReceiveEmails = false; m.TimeOffset = 0; m.MemberSince = createdDate; m.LastVisitDate = createdDate; m.LastPostDate = null; try { Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member"); dal.Add(m); IRoles roleDal = Factory <IRoles> .Create("Role"); roleDal.AddUsersToRoles(new[] { m.Username }, new[] { "all" }); // User creation was a success status = MembershipCreateStatus.Success; // Return the newly craeted user return(GetUserFromMember(m)); } catch { // Something was wrong and the user was rejected status = MembershipCreateStatus.UserRejected; } } else { // There is already a user with this name status = MembershipCreateStatus.DuplicateUserName; } // Something went wrong if we got this far without some sort of status or retun if (status != MembershipCreateStatus.UserRejected && status != MembershipCreateStatus.DuplicateUserName) { status = MembershipCreateStatus.ProviderError; } return(null); }