Ejemplo n.º 1
0
        public async Task <IActionResult> Login([FromBody] LoginModelRequest loginModel)
        {
            ResultResponseObject <TokenResponse> resultService = new ResultResponseObject <TokenResponse>();
            ApplicationUser appUser = null;

            if (loginModel.GrantType == _grantTypePasswordToken)
            {
                appUser = await HandleUserAuthentication(loginModel, resultService);
            }
            else if (loginModel.GrantType == _grantTypeRefreshToken)
            {
                appUser = await HandleRefreshToken(loginModel, resultService);
            }

            if (appUser == null)
            {
                if (resultService.ErrorMessages == null)
                {
                    resultService.ErrorMessages = new List <KeyValuePair <string, string> >();
                }
            }

            if (resultService.Success)
            {
                TokenResponse tokenResponse = await GenerateJwtToken(appUser);

                resultService.Value = tokenResponse;
            }

            return(Response(resultService));
        }
        public async Task <IActionResult> Get(string type)
        {
            var command = new GetWorkItemCommand();

            command.Type = type;

            ResultResponseObject <IEnumerable <WorkItem> > WorkItem = await _workItemApplicationService.Get(command);

            return(Response(WorkItem));
        }
Ejemplo n.º 3
0
        public async Task <ResultResponseObject <IEnumerable <ProductModelResponse> > > GetProductsFilter(string title)
        {
            ResultResponseObject <IEnumerable <ProductModelResponse> > result = new ResultResponseObject <IEnumerable <ProductModelResponse> >();

            IEnumerable <Product> resultService = await _unitOfWork.productRepository.GetProductsFilter(title);

            result.Value = _mapper.Map <IEnumerable <ProductModelResponse> >(resultService);

            return(result);
        }
        public async Task <ResultResponseObject <IEnumerable <WorkItem> > > Get(GetWorkItemCommand command)
        {
            ResultResponseObject <IEnumerable <WorkItem> > resultReponse = new ResultResponseObject <IEnumerable <WorkItem> >();

            using (_uow.Create())
            {
                resultReponse.Value = await _uow.workItemRepository.Get(command);
            }

            return(resultReponse);
        }
        public async Task <ResultResponseObject <IEnumerable <CategoryModelResponse> > > GetAll()
        {
            ResultResponseObject <IEnumerable <CategoryModelResponse> > resultResponse = new ResultResponseObject <IEnumerable <CategoryModelResponse> >();

            IEnumerable <Category> resultService = await _unitOfWork.categoryRepository.Get();

            resultService = resultService.OrderBy(x => x.Title);

            resultResponse.Value = _mapper.Map <IEnumerable <CategoryModelResponse> >(resultService);

            return(resultResponse);
        }
Ejemplo n.º 6
0
        public async Task <ResultResponseObject <ProjectCommandResult> > Get()
        {
            ResultResponseObject <ProjectCommandResult> result = new ResultResponseObject <ProjectCommandResult>();
            Project project = new Project();

            using (_uow.Create())
            {
                project = await _uow.projectRepository.Get();
            }

            result.Value = _mapper.Map <ProjectCommandResult>(project);

            return(result);
        }
        public async Task <IActionResult> Get()
        {
            ResultResponseObject <IEnumerable <CategoryModelResponse> > result = await _categoryApplicationService.GetAll();

            return(Response(result));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Get(string name)
        {
            ResultResponseObject <IEnumerable <ProductModelResponse> > result = await _productApplicationService.GetProductsFilter(name);

            return(Response(result));
        }
Ejemplo n.º 9
0
        private async Task <ApplicationUser> HandleRefreshToken(LoginModelRequest loginModel, ResultResponseObject <TokenResponse> resultResponseModel)
        {
            bool credenciaisValidas = false;

            if (String.IsNullOrEmpty(loginModel.RefreshToken))
            {
                RefreshTokenData refreshTokenBase = null;

                string strTokenArmazenado = _cache.GetString(loginModel.RefreshToken);
                if (!String.IsNullOrWhiteSpace(strTokenArmazenado))
                {
                    refreshTokenBase = JsonConvert
                                       .DeserializeObject <RefreshTokenData>(strTokenArmazenado);
                }

                credenciaisValidas = (refreshTokenBase != null &&
                                      loginModel.RefreshToken == refreshTokenBase.RefreshToken);

                // Elimina o token de refresh já que um novo será gerado
                if (credenciaisValidas)
                {
                    _cache.Remove(loginModel.RefreshToken);
                }
            }

            JwtSecurityToken token = null;

            if (!string.IsNullOrWhiteSpace(loginModel.RefreshToken))
            {
                token = new JwtSecurityTokenHandler().ReadJwtToken(loginModel.RefreshToken);
            }

            if (token == null)
            {
                resultResponseModel.AddError("Refresh Token", "Não foi possível ler o refresh token");
            }
            else if (token.ValidTo < DateTime.Now)
            {
                resultResponseModel.AddError("Refresh Token", "Refresh token inválido");
            }
            else
            {
                string userId = token.Claims.First(claim => claim.Type == JwtRegisteredClaimNames.UniqueName).Value;

                ApplicationUser user = await _userManager.FindByIdAsync(userId);

                return(user);
            }

            return(null);
        }
Ejemplo n.º 10
0
        private async Task <ApplicationUser> HandleUserAuthentication(LoginModelRequest loginModel, ResultResponseObject <TokenResponse> resultResponseModel)
        {
            Microsoft.AspNetCore.Identity.SignInResult result = new Microsoft.AspNetCore.Identity.SignInResult();
            ApplicationUser appUser = _userManager.Users.SingleOrDefault(r => r.UserName == loginModel.Login);

            if (appUser != null)
            {
                result = await _signInManager.PasswordSignInAsync(loginModel.Login, loginModel.Password, false, false);

                if (!result.Succeeded)
                {
                    resultResponseModel.AddError("Login", "Usuário ou senha inválida");
                    appUser = null;
                }
            }
            else
            {
                resultResponseModel.AddError("Login", "Usuário não encontrado");
            }

            return(appUser);
        }
        public async Task <IActionResult> Get(string type)
        {
            ResultResponseObject <ProjectCommandResult> project = await _projectApplicationService.Get();

            return(Response(project));
        }