Exemple #1
0
        public async Task <bool> ChangePassword(ChangePasswordDto input)
        {
            if (_abpSession.UserId == null)
            {
                throw new UserFriendlyException("Please log in before attemping to change password.");
            }
            long userId = _abpSession.UserId.Value;
            User user   = await _userManager.GetUserByIdAsync(userId);

            Abp.Authorization.Users.AbpLoginResult <MultiTenancy.Tenant, User> loginAsync = await _logInManager.LoginAsync(user.UserName, input.CurrentPassword, shouldLockout : false);

            if (loginAsync.Result != AbpLoginResultType.Success)
            {
                throw new UserFriendlyException("Your 'Existing Password' did not match the one on record.  Please try again or contact an administrator for assistance in resetting your password.");
            }
            if (!new Regex(AccountAppService.PasswordRegex).IsMatch(input.NewPassword))
            {
                throw new UserFriendlyException("Passwords must be at least 8 characters, contain a lowercase, uppercase, and number.");
            }
            user.Password = _passwordHasher.HashPassword(user, input.NewPassword);
            CurrentUnitOfWork.SaveChanges();
            return(true);
        }
Exemple #2
0
        public async Task <bool> ResetPassword(ResetPasswordDto input)
        {
            if (_abpSession.UserId == null)
            {
                throw new UserFriendlyException("Please log in before attemping to reset password.");
            }
            long currentUserId = _abpSession.UserId.Value;
            User currentUser   = await _userManager.GetUserByIdAsync(currentUserId);

            Abp.Authorization.Users.AbpLoginResult <MultiTenancy.Tenant, User> loginAsync = await _logInManager.LoginAsync(currentUser.UserName, input.AdminPassword, shouldLockout : false);

            if (loginAsync.Result != AbpLoginResultType.Success)
            {
                throw new UserFriendlyException("Your 'Admin Password' did not match the one on record.  Please try again.");
            }
            if (currentUser.IsDeleted || !currentUser.IsActive)
            {
                return(false);
            }
            IList <string> roles = await _userManager.GetRolesAsync(currentUser);

            if (!roles.Contains(StaticRoleNames.Tenants.Admin))
            {
                throw new UserFriendlyException("Only administrators may reset passwords.");
            }

            User user = await _userManager.GetUserByIdAsync(input.UserId);

            if (user != null)
            {
                user.Password = _passwordHasher.HashPassword(user, input.NewPassword);
                CurrentUnitOfWork.SaveChanges();
            }

            return(true);
        }