コード例 #1
0
        public AuthenticatedViewModel Login(string email, string password)
        {
            if (string.IsNullOrEmpty(email))
            {
                throw new DomainException(ErrorMessage.UserEmailIsEmpty);
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new DomainException(ErrorMessage.PasswordIsRequired);
            }

            var user = _systemUserRepository.GetAll(x => x.Email == email).FirstOrDefault();

            if (user == null)
            {
                throw new DomainException(ErrorMessage.UserIsNotExist);
            }
            if (!user.IsActive)
            {
                throw new DomainException(ErrorMessage.UserWasDisabled);
            }

            if (!PasswordHasher.ValidateHash(password, user.PasswordHash))
            {
                throw new DomainException(ErrorMessage.UserLoginFault);
            }



            using (var unitOfWork = _unitOfWorkFactory.GetCurrentUnitOfWork())
            {
                var authenticationToken = SHA256Hash.CreateHash(Guid.NewGuid().ToString());
                user.UpdateLogin(authenticationToken, _timeSource.LocalNow());


                _systemUserRepository.Edit(user);
                unitOfWork.Commit();

                _cacheManager.Add(authenticationToken, user.Id);
                var authenticatedViewModel = new AuthenticatedViewModel
                {
                    Id    = user.Id,
                    Name  = user.Name,
                    Email = user.Email,
                    AuthenticationToken = authenticationToken,
                    SystemRoleName      = user.SystemRole.Name,
                    SystemRoleId        = user.SystemRoleId
                };

                _cacheManager.Add(user.Id.ToString(), authenticatedViewModel);
                return(authenticatedViewModel);
            }
        }
コード例 #2
0
        public UserViewModel Login(LoginUiCommand command)
        {
            if (IsNullOrEmpty(command.Account) || IsNullOrEmpty(command.Password))
            {
                throw new LogicException(ErrorMessage.UserLoginNull);
            }
            var currentUser = _systemUserRepository.GetAll(x =>
                                                           x.LoginName == command.Account).SingleOrDefault();

            if (currentUser == null)
            {
                throw new LogicException(ErrorMessage.UserIsNotExist);
            }
            if (!PasswordHasher.ValidateHash(command.Password, currentUser.Password))
            {
                throw new LogicException(ErrorMessage.UserLoginFault);
            }

            currentUser.LastLoginDate = _currentTimeProvider.CurrentTime();
            _systemUserRepository.Edit(currentUser);
            var user = currentUser.User.ToLoginViewModel();

            return(user);
        }
コード例 #3
0
 public int UpdateUserInfo(SysUserInfo model)
 {
     return(systemUserRepository.Edit(model));
 }