public UserPasswordHistory SaveUserPreviousPassword(UserPasswordHistory userPassword) { _context.UserPasswordHistory.Add(userPassword); _context.Commit(); return(userPassword); }
/// <summary> /// Changes user password /// </summary> /// <param name="id">User Id</param> /// <param name="currentPassword">CurrentPassword</param> /// <param name="newPassword">Newpassword</param> /// <param name="remarks">List of erros</param> /// <returns>If the operation succeded</returns> public async Task <bool> ChangePassword(Guid id, string currentPassword, string newPassword, Dictionary <string, string> remarks) { var userModel = await _UserDb.GetSingle(id); if (userModel == null || !ValidatePassword(userModel, currentPassword)) { remarks.Add("UserDoesntExistsOrInvalidPws", "User doesn't exists or the password is invalid"); return(false); } var now = DateTime.Now; UserPasswordHistory userPassword = new UserPasswordHistory { Id = Guid.NewGuid(), InitialDate = now, Password = EncriptionHelper.EncriptPassword(newPassword, now) }; if (userModel.PasswordHistory == null) { userModel.PasswordHistory = new List <UserPasswordHistory> { userPassword } } ; else { userModel.PasswordHistory.Add(userPassword); } return(await _UserDb.Update(userModel)); }
/// <summary> /// Adds the user's current password to their password history. /// </summary> public static void UpdateUserPasswordHistory(User user) { UserPasswordHistory uph = UserPasswordHistory.New(); uph.UserId = user.UserId.GetValueOrDefault(); uph.Password = user.Password; uph.Date = DateTime.Now; UserPasswordHistory.Update(uph); }
public UserPasswordHistory SaveUserPreviousPassword(UserPasswordHistory userPassword) { try { return(_userPasswordHistoryRepository.SaveUserPreviousPassword(userPassword)); } catch (Exception e) { Console.WriteLine(e); throw; } }
/// <summary> /// Checks if the user with the specified ID has used the specified password /// recently (ie. within the last 24 password changes). /// Returns true if they have, otherwise false. /// </summary> public static bool IsRecentPassword(User user, string password) { // Get the last 24 passwords UserPasswordHistoryFinder finder = new UserPasswordHistoryFinder { UserId = user.UserId.GetValueOrDefault(-1), MaxRecords = 24 }; finder.SortExpressions.Add(new DescendingSort(UserPasswordHistory.Columns.Date)); EntityList <UserPasswordHistory> passwordHistory = UserPasswordHistory.FindMany(finder); return(passwordHistory.Any(uph => StringHasher.VerifyHash(uph.Password, password + user.PasswordSalt))); }