Exemple #1
0
        public async Task <IActionResult> GetAllProductsAsync(
            [FromServices] IProductsAppService productsAppService
            )
        {
            var search = await productsAppService.GetAllProductsAsync();

            var data            = _mapper.Map <List <ProductsModelView> >(search);
            var returnModelView = new BaseViewModel <List <ProductsModelView> >
            {
                Mensagem        = data.Count > 0?  messages.PRODUCTS_ALL_SUCCESS(data.Count) : messages.PRODUCTS_ALL_FAIL(),
                Sucesso         = true,
                ObjetoDeRetorno = data
            };

            #region .::Log Requests
            loggerService.SaveLoggerSuccess(new LoggerDomain
            {
                objects = JsonConvert.SerializeObject(returnModelView),
                userId  = UsuarioId
            });
            #endregion

            return(Ok(returnModelView));
        }
Exemple #2
0
        public async Task <IActionResult> Login(
            [FromBody] UserViewModel usuario,
            [FromServices] TokenConfigurations tokenConfiguration,
            [FromServices] SignConfigurationToken signinConfiguration,
            [FromServices] IUserAppService usuarioAppService,
            [FromServices] ILoggerAppService loggerService)
        {
            if (usuario is null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var objRetorno = new BaseViewModel <TokenViewModel>();

            var viewModel = _mapper.Map <UserDomain>(usuario);

            var userBase = await usuarioAppService.ValidarUsuarioAsync(viewModel);

            if (userBase != null)
            {
                var identity = new ClaimsIdentity(
                    new GenericIdentity(userBase.Login, "Login"),
                    new[]
                {
                    new Claim(JwtRegisteredClaimNames.Jti, userBase.UserId.ToString()),
                    new Claim(JwtRegisteredClaimNames.UniqueName, userBase.Email),
                    new Claim("CompanyId", userBase.CompanyId.ToString())
                }
                    );

                var dateCreate  = DateTime.Now;
                var dateExpired = dateCreate + TimeSpan.FromDays(tokenConfiguration.ExpireIn);

                var handler = new JwtSecurityTokenHandler();

                var securityToken = handler.CreateToken(new SecurityTokenDescriptor
                {
                    Issuer    = tokenConfiguration.Issuer,
                    Audience  = tokenConfiguration.Audience,
                    Subject   = identity,
                    NotBefore = dateCreate,
                    Expires   = dateExpired,

                    SigningCredentials = new SigningCredentials(
                        new SymmetricSecurityKey(
                            Encoding.UTF8.GetBytes(tokenConfiguration.SigningKey)),
                        SecurityAlgorithms.HmacSha256
                        )
                });

                var token = handler.WriteToken(securityToken);
                objRetorno.ObjetoDeRetorno = new TokenViewModel()
                {
                    UsuarioId   = userBase.UserId,
                    Nome        = userBase.Name,
                    Email       = userBase.Email,
                    Autenticado = true,
                    Criacao     = dateCreate,
                    Expira      = dateExpired,
                    Token       = token,
                };
                #region Logger
                loggerService.SaveLoggerSuccess(new domain.core.Entity.Log.LoggerDomain
                {
                    objects = JsonConvert.SerializeObject(objRetorno.ObjetoDeRetorno),
                    token   = objRetorno.ObjetoDeRetorno.Token,
                    userId  = objRetorno.ObjetoDeRetorno.UsuarioId
                });
                #endregion
                return(Ok(objRetorno));
            }

            return(Unauthorized());
        }