Exemple #1
0
        public async Task AddAsync(UserDomain user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (user.Password != user.ConfirmPassword)
            {
                throw new InvalidOperationException("Password and confirmation password do not match");
            }

            if (string.IsNullOrEmpty(user.Img))
            {
                user.Img = ConfigurationManager.AppSettings["DefaultImageUrl"];
            }

            var newUser = new ApplicationUser
            {
                Password = PasswordHasher.HashPassword(user.Password),
                Email    = user.Email,
                UserName = user.UserName,
                Img      = user.Img,
                Language = await _languageRepository.GetByLanguageTypeAsync(user.Language.LanguageType)
            };

            _authContext.Users.Add(newUser);
            await _authContext.SaveChangesAsync();
        }
Exemple #2
0
        public async Task <ApplicationUser> FindAsync(string userName, string password)
        {
            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentException(nameof(userName));
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentException(nameof(password));
            }

            var users = await _authContext.Users.Where(x => x.UserName == userName).ToListAsync();

            return(users.FirstOrDefault(x => PasswordHasher.VerifyHashedPassword(x.Password, password)));
        }
Exemple #3
0
        public async Task UpdateAsync(UserDomain user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (user.Password != user.ConfirmPassword)
            {
                throw new InvalidOperationException("Password and confirmation password do not match");
            }

            var existingUser = await GetByIdAsync(user.Id);

            if (existingUser == null)
            {
                throw new InvalidOperationException("user is not found");
            }

            var isVerifiedPassword = PasswordHasher
                                     .VerifyHashedPassword(existingUser.Password, user.OldPassword);

            if (!isVerifiedPassword)
            {
                throw new InvalidOperationException("Incorrect password");
            }

            if (user.Img != null && !user.Img.IsUrl())
            {
                existingUser.Img = await ImageUploader.UploadAsync(user.Img);
            }

            existingUser.Email    = user.Email;
            existingUser.UserName = user.UserName;
            existingUser.Password = PasswordHasher.HashPassword(user.Password);
            existingUser.Language = await _languageRepository.GetByLanguageTypeAsync(user.Language.LanguageType);

            await _authContext.SaveChangesAsync();
        }