public void Handle(RegisterUserCommand command)
 {
     command.User.Salt     = HashPassword.GenerateSalt(50);
     command.User.Password = HashPassword.GenerateHash(command.User.Password, command.User.Salt);
     using (_unitOfWork = new UnitOfWork())
     {
         _handler.Handle(command);
     }
 }
Esempio n. 2
0
        public User Handle(GetUserUnderUsernameAndPasswordQuery query)
        {
            User returnUser;


            using (_unitOfWork = new UnitOfWork())
            {
                string _password = HashPassword.GenerateHash(query.User.Password, _salt);
                query.User.Password = _password;
                returnUser          = _handler.Handle(query);
            }

            return(returnUser);
        }
Esempio n. 3
0
        public IActionResult InserirAdmin([FromBody] UsuariosDomain Usuarios)
        {
            var users = _context.Usuarios;

            if (Usuarios == null)
            {
                return(BadRequest("Dados do usuário incorretos"));
            }
            foreach (var usua in users)
            {
                if (Usuarios.Email == usua.Email)
                {
                    return(BadRequest("E-mail já cadastrado"));
                }
            }

            try
            {
                HashPassword geradorHash = new HashPassword();
                var          hash        = geradorHash.GenerateHash(Usuarios.Senha);
                if (hash != null)
                {
                    Usuarios.Senha = hash;
                }

                _context.Usuarios.Add(Usuarios);
                _context.SaveChanges();

                UsuarioPermissoesDomain permissoes = new UsuarioPermissoesDomain();
                permissoes.UsuarioId   = Usuarios.id;
                permissoes.PermissaoId = 1;
                permissoes.DataCriacao = DateTime.Now;
                _context.UsuarioPermissoes.Add(permissoes);
                _context.SaveChanges();
                return(Ok("Cadastrado com sucesso"));
            }
            catch (System.Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Esempio n. 4
0
        public object Login([FromBody] UsuariosDomain usuario, [FromServices] signingConfigurations signingConfigurations, [FromServices] TokenConfigurations tokenConfigurations)
        {
            try
            {
                HashPassword geradorHash = new HashPassword();
                var          hash        = geradorHash.GenerateHash(usuario.Senha);
                if (hash != null)
                {
                    usuario.Senha = hash;
                }

                UsuariosDomain         user      = contexto.Usuarios.Include("UsuarioPermissoes").Include("UsuarioPermissoes.Permissao").FirstOrDefault(c => c.Email == usuario.Email && c.Senha == usuario.Senha);
                List <RespostasDomain> respostas = contexto.Respostas.Where(a => a.UsuarioId == user.id).ToList();

                if (user != null)
                {
                    ClaimsIdentity identity = new ClaimsIdentity(new GenericIdentity(user.id.ToString(), "Login"), new[] {
                        new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")),
                        new Claim(JwtRegisteredClaimNames.UniqueName, user.id.ToString()),
                        new Claim("Nome", user.Nome),
                        new Claim("Id", user.id.ToString()),
                        new Claim(ClaimTypes.Email, user.Email)
                    });

                    var claims = new List <Claim>();
                    foreach (var item in user.UsuarioPermissoes)
                    {
                        claims.Add(new Claim(ClaimTypes.Role, item.Permissao.Nome));
                    }

                    identity.AddClaims(claims);

                    var handler       = new JwtSecurityTokenHandler();
                    var securityToken = handler.CreateToken(new SecurityTokenDescriptor
                    {
                        Issuer             = tokenConfigurations.Issuer,
                        Audience           = tokenConfigurations.Audience,
                        SigningCredentials = signingConfigurations.SigningCredentials,
                        Subject            = identity,
                    });

                    var token = handler.WriteToken(securityToken);

                    var respostaJson = new
                    {
                        respostas = respostas.Count(),
                        user.id,
                        user.Nome,
                        permissoes = user.UsuarioPermissoes.Select(d => new
                        {
                            d.Permissao.Nome
                        }).ToArray()
                    };

                    var retorno = new { atutenticacao = true, acessToken = token, message = "OK", usuario = respostaJson };

                    return(Ok(retorno));
                }

                var retornoerro = new { autenticacao = false, message = "Falha na Autenticação" };
                return(BadRequest(retornoerro));
            }
            catch (System.Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }