Example #1
0
        public async Task <ServiceResponse <UserDto> > PasswordChange(PasswordChangeDto pwdChange)
        {
            ServiceResponse <UserDto> serviceResponse = new ServiceResponse <UserDto>();
            PasswordHashing           ph = new PasswordHashing();
            User entity = new User();

            try
            {
                entity = _context.Users.First(u => u.Username == pwdChange.Username);
                if (ph.IsValid(pwdChange.CurrentPassword, entity.Salt, entity.Password))
                {
                    entity.Salt                  = Encoding.Unicode.GetString(ph.GetSalt());
                    entity.Password              = Encoding.Unicode.GetString(ph.GetKey(pwdChange.NewPassword, Encoding.Unicode.GetBytes(entity.Salt)));
                    entity.LastPasswordChange    = DateTime.Now;
                    entity.EnforcePasswordChange = false;
                    _context.SaveChanges();
                    serviceResponse.Data = _mapper.Map <UserDto>(entity);
                }
                else
                {
                    throw new Exception("Wrong current password!");
                }
            }
            catch (Exception ex)
            {
                serviceResponse.Success = false;
                serviceResponse.Message = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
            }
            return(serviceResponse);
        }
Example #2
0
        public async Task <ServiceResponse <UserDto> > AddUser(AddUserDto newUser)
        {
            ServiceResponse <UserDto> serviceResponse = new ServiceResponse <UserDto>();
            User            user = new User();
            PasswordHashing ph   = new PasswordHashing();

            try
            {
                user          = _mapper.Map <User>(newUser);
                user.Salt     = Encoding.Unicode.GetString(ph.GetSalt());
                user.Password = Encoding.Unicode.GetString(ph.GetKey(user.Password, Encoding.Unicode.GetBytes(user.Salt)));
                _context.Users.Add(user);
                _context.SaveChanges();
                serviceResponse.Data = _mapper.Map <UserDto>(user);
            }
            catch (Exception ex)
            {
                serviceResponse.Success = false;
                serviceResponse.Message = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
            }
            return(serviceResponse);
        }