Beispiel #1
0
        public IActionResult Authenticate([FromBody] AuthenticateIn value)
        {
            if (value.Nome == "Wilham Ezequiel de Sousa" && value.Senha == "123")
            {
                var securityKey = Configuration["SecurityKey"];

                var handler = new UsuarioHandler();
                var command = new UsuarioAuthenticateCommand()
                {
                    Nome = value.Nome, Senha = value.Senha, SecurityKey = securityKey
                };
                var result = (CommandResult <string>)handler.Handle(command, Service);
                return(Ok(result));
            }
            else
            {
                bool sucess = false;
                var  result = new CommandResult <bool>()
                {
                    Success = false, Message = "Usuário ou senha inválidos"
                };
                result.Data = sucess;
                return(Unauthorized(result));
            }
        }
        public ICommandResult Handle(UsuarioAuthenticateCommand command, IServiceProvider service)
        {
            //Regras e Fluxo
            command.Validate();
            if (command.Invalid)
            {
                AddNotifications(command);
                var message = "Não foi possível autenticar. \n";
                foreach (var notification in command.Notifications)
                {
                    message += $"{notification.Property} - {notification.Message}" + "\n";
                }

                return(new CommandResult <string>(false, message));
            }

            var usuarioRepository = (IUsuarioRepository)service.GetService(typeof(IUsuarioRepository));
            var usuario           = usuarioRepository.Read(command.Nome);

            if (usuario == null)
            {
                return(new CommandResult <string>(false, "Usuário não encontrado."));
            }

            if (usuario.Senha != command.Senha)
            {
                return(new CommandResult <string>(false, "Senha inválida."));
            }

            //criar token
            var claims = new[]
            {
                new Claim(ClaimTypes.Name, command.Nome)
            };

            //recebe uma instancia da classe SymmetricSecurityKey
            //armazenando a chave de criptografia usada na criação do token
            var key = new SymmetricSecurityKey(
                Encoding.UTF8.GetBytes(command.SecurityKey));

            //assinar digitalmente
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var token = new JwtSecurityToken(
                issuer: "juntoseguros",
                audience: "juntoseguros",
                claims: claims,
                expires: DateTime.Now.AddMinutes(30),
                signingCredentials: creds);

            var tokenAssinado = new JwtSecurityTokenHandler().WriteToken(token);

            var result = new CommandResult <string>(true, "Usuário autenticado com sucesso.");

            result.Data = tokenAssinado;
            return(result);
        }
Beispiel #3
0
        public void AutheticateSuccess()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioAuthenticateCommand()
            {
                SecurityKey = key, Nome = "UsuarioExistente", Senha = "123"
            };
            var result = (CommandResult <string>)handler.Handle(command, Service);

            Assert.AreEqual(true, result.Success);
        }