コード例 #1
0
        public IActionResult Logar(LoginViewModelInput loginViewModelInput)
        {
            var usuarioViewModelOutput = new UsuarioViewModelOutput()
            {
                Codigo = 1,
                Login  = "******",
                Email  = "*****@*****.**"
            };

            var secret = Encoding.ASCII.GetBytes("QwErTyUiOp357159@#$");
            var symmetricSecurityKey    = new SymmetricSecurityKey(secret);
            var securityTokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.NameIdentifier, usuarioViewModelOutput.Codigo.ToString()),
                    new Claim(ClaimTypes.Name, usuarioViewModelOutput.Login.ToString()),
                    new Claim(ClaimTypes.Email, usuarioViewModelOutput.Email.ToString())
                }),
                Expires            = DateTime.UtcNow.AddDays(1),
                SigningCredentials = new SigningCredentials(symmetricSecurityKey, SecurityAlgorithms.HmacSha256Signature)
            };

            var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
            var tokenGenerated          = jwtSecurityTokenHandler.CreateToken(securityTokenDescriptor);
            var token = jwtSecurityTokenHandler.WriteToken(tokenGenerated);

            return(Ok(new
            {
                Token = token,
                Usuario = usuarioViewModelOutput
            }));
        }
コード例 #2
0
        public IActionResult Logar(LoginViewModelInput loginViewModelInput)
        {
            var usuario = _usuarioRepository.ObterUsuario(loginViewModelInput.Login);

            if (usuario == null)
            {
                return(BadRequest("Houve um erro ao tentar acessar. (Usuário não encontrado ou inexistente)"));
            }
            if (usuario.Senha != loginViewModelInput.Senha)
            {
                return(BadRequest("Houve um erro ao tentar acessar. (Senha incorreta)"));
            }

            var usuarioViewModelOutput = new UsuarioViewModelOutput()
            {
                Codigo = usuario.Codigo,
                Login  = loginViewModelInput.Login,
                Email  = usuario.Email
            };
            var token = _authenticationService.GerarToken(usuarioViewModelOutput);

            return(Ok(new
            {
                Token = token,
                Usuario = usuarioViewModelOutput
            }));
        }
コード例 #3
0
        public IActionResult Logar(LoginViewModelInput loginViewModelInput)
        {
            var usuario = _usuarioRepository.ObterUsuario(loginViewModelInput.Login);

            if (usuario == null)
            {
                return(BadRequest("Houve um erro ao tentar acessar."));
            }

            //if (usuario.Senha != loginViewModel.Senha.GerarSenhaCriptografada())
            //{
            //    return BadRequest("Houve um erro ao tentar acessar.");
            //}

            var usuarioViewModelOutput = new UsuarioViewModelOutput()
            {
                Codigo = usuario.Codigo,
                Login  = loginViewModelInput.Login,
                Email  = usuario.Email
            };

            var token = _authenticationService.GerarToken(usuarioViewModelOutput);

            return(Ok(new
            {
                Token = token,
                Usuario = usuarioViewModelOutput
            }));
        }
コード例 #4
0
        public IActionResult Logar(LoginViewModelInput loginViewModelInput)
        {
            var usuarioViewModelOutput = new UsuarioViewModelOutput()
            {
                Codigo = 1,
                Login  = "******",
                Email  = "*****@*****.**"
            };

            var secret = Encoding.ASCII.GetBytes("MzfsT&d9gprP>!9$Es(X!5g@;ef!5sbk:jH\\2.}8ZP'qY#7");
            var symmetricSecurityKey    = new SymmetricSecurityKey(secret);
            var securityTokenDescriptor = new SecurityTokenDescriptor {
                Subject = new ClaimsIdentity(new Claim[] {
                    new Claim(ClaimTypes.NameIdentifier, usuarioViewModelOutput.Codigo.ToString()),
                    new Claim(ClaimTypes.Name, usuarioViewModelOutput.Login.ToString()),
                    new Claim(ClaimTypes.Email, usuarioViewModelOutput.Email.ToString())
                }),
                Expires            = DateTime.UtcNow.AddDays(1),
                SigningCredentials = new SigningCredentials(symmetricSecurityKey, SecurityAlgorithms.HmacSha256Signature)
            };

            var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
            var tokenGenerated          = jwtSecurityTokenHandler.CreateToken(securityTokenDescriptor);
            var token = jwtSecurityTokenHandler.WriteToken(tokenGenerated);

            return(Ok(new {
                Token = token,
                Usuario = usuarioViewModelOutput
            }));
        }
コード例 #5
0
        public IActionResult Logar(LoginViewModelInput loginViewModelInput)
        {
            //if (!ModelState.IsValid)
            //{
            //    return BadRequest(new ValidaCampoViewModelOutput(ModelState.SelectMany(sm => sm.Value.Errors).Select(s => s.ErrorMessage)));
            //} Linha comentada pois iramos criar uma pasta(Filters) só para fazer isso

            var usuarioViewModelOutput = new UsuarioViewModelOutput() // Serve para gerar o token
            {
                Codigo = 1,
                Login  = "******",
                Email  = "*****@*****.**"
            };


            var tokenService = new Services.TokenService();

            var token = tokenService.GenerateToken(usuarioViewModelOutput);

            return(Ok(new
            {
                Token = token,
                Usuario = loginViewModelInput
            }));
        }
コード例 #6
0
        public IActionResult Logar(LoginViewModelInput loginViewModelInput)
        {
            var usuario = _usuarioRepository.ObterUsuario(loginViewModelInput.Login);

            if (usuario == null)
            {
                return(BadRequest("Houve um erro ao tentar acessar."));
            }
            //if(usuario.Senha != loginViewModelInput.Senha.GerarSenhaCriptografada())
            //{
            //    return BadRequest("Houve um erro ao tentar acessar.");
            //}

            //if (!ModelState.IsValid)
            //{
            //    return BadRequest(new ValidaCampoViewModelOutput(ModelState.SelectMany(sm => sm.Value.Errors).Select(s => s.ErrorMessage)));
            //}

            var usuarioViewModelOutput = new UsuarioViewModelOutput()
            {
                Codigo = usuario.Codigo,
                Login  = loginViewModelInput.Login,
                Email  = usuario.Email
            };

            var token = _authenticationService.GerarToken(usuarioViewModelOutput);


            return(Ok(new
            {
                Token = token,
                Usuario = usuarioViewModelOutput
            }));
        }
コード例 #7
0
        [ValidacaoModelStateCustomizado] //usa filters ouves das linhas cruas abaixo de Logar()
        public IActionResult Logar(LoginViewModelInput loginViewModelInput)
        {
            Usuario usuario = _usuarioRepository.ObterUsuario(loginViewModelInput.Login);

            if (usuario == null)
            {
                return(BadRequest("Houve um erro ao tentar acessar."));
            }

            /* if(usuario.Senha != loginViewModel.Senha.GerarSenhaCriptografada())
             * {
             *  return BadRequest("Houve um erro ao tentar acessar.");
             * }*/
            var usuarioViewModelOutput = new UsuarioViewModelOutput()//cria um novo usuario local pra testar
            {
                Codigo = usuario.Codigo,
                Login  = loginViewModelInput.Login,
                Email  = usuario.Email
            };

            var token = _authenticationService.GerarToken(usuarioViewModelOutput);//devolve o token escrito de fato para essa var que vai ser chamada abaixo

            return(Ok(new
            {
                Token = token,
                Usuario = usuarioViewModelOutput
            }));//funfou, retorna o token e o usuarioviewoutput
        }
コード例 #8
0
        public IActionResult Logar(LoginViewModelInput loginViewModelInput)
        {
            var usuario = _repository.ObterUsuario(loginViewModelInput);

            if (usuario == null)
            {
                return(BadRequest("Login não localizado."));
            }

            var usuarioOutput = new UsuarioViewModelOutput
            {
                Codigo = usuario.Codigo,
                Email  = usuario.Email,
                Login  = usuario.Login
            };

            var token = _authentication.GerarToken(usuarioOutput);

            return(Ok(new { Token = token, Usuario = usuarioOutput }));
        }
        public IActionResult Logar(LoginViewModelInput loginViewModelInput)
        {
            // manual user creation only for tests, remove after creating DB connection
            var usuarioViewModelOutput = new UsuarioViewModelOutput()
            {
                Codigo = 1,
                Login  = "******",
                Email  = "*****@*****.**"
            };

            var secret = Encoding.ASCII.GetBytes("@_S3cReT_T0kEn_@");
            var symmetricSecurityKey = new SymmetricSecurityKey(secret);

            // configuration of the descriptor of the token data received
            var securityTokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.NameIdentifier, usuarioViewModelOutput.Codigo.ToString()),
                    new Claim(ClaimTypes.Name, usuarioViewModelOutput.Login.ToString()),
                    new Claim(ClaimTypes.Email, usuarioViewModelOutput.Email.ToString())
                }),

                Expires            = DateTime.UtcNow.AddDays(1),
                SigningCredentials = new SigningCredentials(symmetricSecurityKey, SecurityAlgorithms.HmacSha256Signature)
            };

            // token generation using the descriptor definition
            var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
            var tokenGenerated          = jwtSecurityTokenHandler.CreateToken(securityTokenDescriptor);
            var token = jwtSecurityTokenHandler.WriteToken(tokenGenerated);

            return(Ok(new
            {
                Token = token,
                Usuario = usuarioViewModelOutput
            }));
        }
コード例 #10
0
ファイル: Usuario.cs プロジェクト: AdrianoSantana/dotnet
        public IActionResult Logar(LoginViewModelInput loginViewModelInput)
        {
            var user = _usuarioRepository.getUser(loginViewModelInput.Login);

            if (user == null)
            {
                return(BadRequest("Ocorreu um erro"));
            }

            var usuarioViewModelOutput = new UsuarioViewModelOutput()
            {
                Codigo = user.Codigo,
                Login  = loginViewModelInput.Login,
                Email  = user.Email
            };

            var token = _authenticationService.GetToken(usuarioViewModelOutput);

            return(Ok(new {
                Token = token,
                Usuario = usuarioViewModelOutput
            }));
        }
コード例 #11
0
        public IActionResult Logar(LoginViewModelInput loginViewModelInput)
        {
            var usuario = _usuarioRepository.ObterUsuario(loginViewModelInput.Login);

            if (usuario == null)
            {
                return(BadRequest("Houve um erro ao tentar acessar"));
            }

            var usuarioViewModelOutput = new UsuarioViewModelOutput()
            {
                Codigo = usuario.Codigo,
                Login  = usuario.Login,
                Email  = usuario.Email
            };

            var token = _autheitication.GerarToken(usuarioViewModelOutput);

            return(Ok(new {
                Token = token,
                Usuario = usuarioViewModelOutput
            }));
        }
コード例 #12
0
        public async Task <IActionResult> Logar(LoginViewModelInput loginViewModelInput)
        {
            try
            {
                var usuario = await _usuarioRepository.ObterUsuarioAsync(loginViewModelInput.Login);

                if (usuario == null)
                {
                    return(BadRequest("Houve um erro ao tentar acessar."));
                }

                //if (usuario.Senha != loginViewModel.Senha.GerarSenhaCriptografada())
                //{
                //    return BadRequest("Houve um erro ao tentar acessar.");
                //}

                var usuarioViewModelOutput = new UsuarioViewModelOutput()
                {
                    Codigo = usuario.Codigo,
                    Login  = loginViewModelInput.Login,
                    Email  = usuario.Email
                };

                var token = _authenticationService.GerarToken(usuarioViewModelOutput);

                return(Ok(new LoginViewModelOutput
                {
                    Token = token,
                    Usuario = usuarioViewModelOutput
                }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                return(new StatusCodeResult(500));
            }
        }