Ejemplo n.º 1
0
        /// <summary>
        /// Force resets a user password to a specified one
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="newPass"></param>
        /// <returns></returns>
        public static async Task <ForceResetPasswordOutput> ForceResetPasswordAsync(Guid userId, string newPass)
        {
            var output = new ForceResetPasswordOutput();

            if (string.IsNullOrWhiteSpace(newPass))
            {
                output.FailureReason = "new_pass_null";
                return(output);
            }

            try
            {
                var userManager = MapHive.Core.Identity.UserManagerUtils.GetUserManager();
                var idUser      = await userManager.FindByIdAsync(userId.ToString());

                if (idUser != null)
                {
                    if (await userManager.CheckPasswordAsync(idUser, newPass))
                    {
                        output.FailureReason = "new_pass_same_as_old_pass";
                        output.Success       = false;
                    }
                    else
                    {
                        var passResetToken = await userManager.GeneratePasswordResetTokenAsync(idUser);

                        await userManager.ResetPasswordAsync(idUser, passResetToken, newPass);

                        output.Success = true;
                    }
                }
            }
            catch (Exception ex)
            {
                output.FailureReason = "unknown_error";
            }

            return(output);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Force resets a user password to a specified one
        /// </summary>
        /// <typeparam name="TAccount"></typeparam>
        /// <param name="userAccountService"></param>
        /// <param name="userId"></param>
        /// <param name="newPass"></param>
        /// <returns></returns>
        public static async Task <ForceResetPasswordOutput> ForceResetPasswordAsync <TAccount>(
            UserAccountService <TAccount> userAccountService, Guid userId, string newPass)
            where TAccount : RelationalUserAccount
        {
            var output = new ForceResetPasswordOutput();

            if (string.IsNullOrWhiteSpace(newPass))
            {
                output.FailureReason = "new_pass_null";
                return(output);
            }

            try
            {
                PasswordResetRequestedEvent <TAccount> e = null;
                userAccountService.Configuration.AddEventHandler(new MembershipRebootEventHandlers.PasswordResetRequestedEventHandler <TAccount>
                                                                     ((evt) =>
                {
                    e = evt;
                })
                                                                 );
                userAccountService.ResetPassword(userId);

                //got the reset token, so can now change the pass..
                output.Success = userAccountService.ChangePasswordFromResetKey(e.VerificationKey, newPass);
            }
            catch (Exception ex)
            {
                if (ex.Message == "The new password must be different from the old password.")
                {
                    output.FailureReason = "new_pass_same_as_old_pass";
                }
            }

            return(output);
        }