/// <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> /// 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> /// 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> /// 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); }
/// <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); }