Beispiel #1
0
        public async Task <bool> ChangePasswordAsync(ChangePasswordDto changePasswordDto)
        {
            ValidatorUserService.ValidatorChangePassword(changePasswordDto);

            var user = await this.context.Users
                       .Where(userPaswword => userPaswword.Id == changePasswordDto.UserId)
                       .FirstOrDefaultAsync();

            ValidatorUserService.ValidatorExistUser(user);

            var passwordHasher = new PasswordHasher <User>();

            user.PasswordHash = user.PasswordHash = passwordHasher.HashPassword(user, changePasswordDto.NewPassword);
            user.FirstLog     = true;

            await this.context.SaveChangesAsync();

            return(true);
        }
Beispiel #2
0
        public async Task RegisterAccountAsync(RegisterAccountDto registerAccountDto)
        {
            ValidatorUserService.ValidatorRoleName(registerAccountDto);
            ValidatorUserService.ValidatorUserName(registerAccountDto);
            ValidatorUserService.ValidatorPassword(registerAccountDto);
            ValidatorUserService.ValidatorUserEmail(registerAccountDto);

            var user = await this.context.Users
                       .Where(name => name.UserName == registerAccountDto.UserName)
                       .Select(username => username.UserName)
                       .SingleOrDefaultAsync();

            ValidatorUserService.ValidatorForExistUsername(user, registerAccountDto);

            var passwordHasher = new PasswordHasher <User>();

            var account = new User
            {
                UserName           = registerAccountDto.UserName,
                NormalizedUserName = registerAccountDto.UserName.ToUpper(),
                LockoutEnabled     = true,
                Email           = registerAccountDto.Email,
                NormalizedEmail = registerAccountDto.Email.ToUpper()
            };

            account.PasswordHash = passwordHasher.HashPassword(account, registerAccountDto.Password);

            await this.userManager.CreateAsync(account);

            await this.userManager.AddToRoleAsync(account, registerAccountDto.Role);

            var currentUser = await this.context.Users
                              .Where(userId => userId.Id == registerAccountDto.CurrentUserId)
                              .Select(uName => uName.UserName)
                              .SingleOrDefaultAsync();

            logger.LogInformation($"New user {registerAccountDto.UserName} registered by {currentUser}");
        }