Example #1
0
        public BaseViewModel <TokenViewModel> Login(LoginViewModel user)
        {
            var entity = _repository.GetById(user.Username);

            if (entity == null)
            {
                return(new BaseViewModel <TokenViewModel>
                {
                    StatusCode = HttpStatusCode.NotFound,
                    Description = ErrMessageConstants.ACCOUNT_NOTFOUND,
                    Code = ErrMessageConstants.ACCOUNT_NOTFOUND,
                    Data = null
                });
            }
            if (!SaltHashPassword.Verify(entity.SaltPassword, entity.HashPassword, user.Password))
            {
                return(new BaseViewModel <TokenViewModel>
                {
                    StatusCode = HttpStatusCode.PreconditionFailed,
                    Description = ErrMessageConstants.INVALID_ACCOUNT,
                    Code = ErrMessageConstants.INVALID_ACCOUNT,
                    Data = null
                });
            }
            var result = authenticate(entity);

            return(new BaseViewModel <TokenViewModel>
            {
                StatusCode = HttpStatusCode.OK,
                Description = null,
                Code = MessageConstants.SUCCESS,
                Data = result
            });
        }
Example #2
0
        public BaseViewModel <TokenViewModel> Register(RegisterViewModel user)
        {
            var check = _repository.GetById(user.Username);

            if (check != null)
            {
                return(new BaseViewModel <TokenViewModel>()
                {
                    Data = null,
                    Code = MessageConstants.FAILURE,
                    Description = ErrMessageConstants.ACCOUNT_EXISTED,
                    StatusCode = HttpStatusCode.PreconditionFailed
                });
            }
            var entity = new Account
            {
                Username = user.Username,
                Fullname = user.FullName,
                Email    = user.Email,
                Avartar  = user.Avartar,
                Phone    = user.Phone
            };
            var temp = new SaltHashPassword(user.Password);

            entity.SaltPassword = temp.Salt;
            entity.HashPassword = temp.Hash;
            entity.Role         = Role.User;
            entity.IsDelete     = false;

            _repository.Add(entity);
            Save();
            var result = authenticate(entity);

            return(new BaseViewModel <TokenViewModel>()
            {
                Data = result,
                Code = MessageConstants.SUCCESS,
                Description = null,
                StatusCode = HttpStatusCode.Created
            });
        }