Exemple #1
0
        /// <summary>
        /// Change the password service.
        /// </summary>
        /// <param name="primKey">the primary key of the user to be changed</param>
        /// <returns></returns>
        public async Task ChangePasswordForUser(int primKey)
        {
            //TODO: refactor -> make unit test, return result
            //TODO: generate client class for frontend
            User userToBeUpdated = null;

            //why iterate over user?
            foreach (User us in Users)
            {
                if (us.Id == primKey)
                {
                    userToBeUpdated = us;
                    break;
                }
            }
            if (userToBeUpdated != null && !string.IsNullOrEmpty(userToBeUpdated.Email))
            {
                string newPassword = new PasswordGenerator(PasswordGuidelines.RequiredMinLength, PasswordGuidelines.GetMaximumLength(),
                                                           PasswordGuidelines.GetAmountOfLowerLetters(), PasswordGuidelines.GetAmountOfUpperLetters(), PasswordGuidelines.GetAmountOfNumerics(),
                                                           PasswordGuidelines.GetAmountOfSpecialChars()).Generate();
                await _userManager.ChangePasswordAsync(userToBeUpdated, newPassword);

                mailProvider.PasswordReset(newPassword, userToBeUpdated.Email);
            }
        }
        public async Task <IActionResult> UpdatePassword([FromQuery] long id, [FromQuery] String newPassword)
        {
            if (!PasswordGuidelines.IsPasswordWithinRestrictions(newPassword))
            {
                string text = "Passwort sollte eine Länge haben von " + PasswordGuidelines.RequiredMinLength + " bis zu " + PasswordGuidelines.MaxLength + " Zeichen" +
                              (PasswordGuidelines.RequireDigit ? ", eine Anzahl von " + PasswordGuidelines.GetAmountOfNumerics() + " Zahlen" : "") +
                              (PasswordGuidelines.RequireNonAlphanumeric ? ", eine Anzahl von " + PasswordGuidelines.GetAmountOfSpecialChars() + " Sonderzeichen" : "") +
                              (PasswordGuidelines.RequireLowercase ? ", eine Anzahl von " + PasswordGuidelines.GetAmountOfLowerLetters() + " kleinen Buchstaben" : "") +
                              (PasswordGuidelines.RequireUppercase ? ", eine Anzahl von " + PasswordGuidelines.GetAmountOfUpperLetters() + " großen Buchstaben." : "");
                return(Conflict(text));
            }
            IdentityResult result = await _userService.ChangePasswordForUserAsync(id, newPassword).ConfigureAwait(false);

            if (result.Succeeded)
            {
                return(Ok(true));
            }
            else
            {
                return(NotFound());
            }
        }