Exemplo n.º 1
0
        public List <AD> BuscaUsuario(string Dominio, string LoginUsuario)
        {
            PrincipalContext pc      = new PrincipalContext(ContextType.Domain, Dominio);
            List <AD>        usuario = new List <AD>();

            using (UserPrincipal user1 = new UserPrincipal(pc))
            {
                user1.SamAccountName = LoginUsuario;
                using (var searcher = new PrincipalSearcher(user1))
                {
                    foreach (var result in searcher.FindAll())
                    {
                        DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
                        string         _Email;
                        if (de.Properties["mail"].Value == null)
                        {
                            _Email = "";
                        }
                        else
                        {
                            _Email = de.Properties["mail"].Value.ToString();
                        }

                        string _Empresa;
                        if (de.Properties["company"].Value == null)
                        {
                            _Empresa = "";
                        }
                        else
                        {
                            _Empresa = de.Properties["company"].Value.ToString();
                        }

                        string _Departamento;
                        if (de.Properties["department"].Value == null)
                        {
                            _Departamento = "";
                        }
                        else
                        {
                            _Departamento = de.Properties["department"].Value.ToString();
                        }

                        string _Cargo;
                        if (de.Properties["title"].Value == null)
                        {
                            _Cargo = "";
                        }
                        else
                        {
                            _Cargo = de.Properties["title"].Value.ToString();
                        }

                        AD ad = new AD()
                        {
                            Nome         = de.Properties["name"].Value.ToString(),
                            Login        = de.Properties["sAMAccountName"].Value.ToString(),
                            Email        = _Email,
                            Empresa      = _Empresa,
                            Departamento = _Departamento,
                            Cargo        = _Cargo,
                        };
                        usuario.Add(ad);
                    }
                }
            }
            return(usuario);
        }
Exemplo n.º 2
0
        public List <AD> BuscaUsuarios(string Dominio)
        {
            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Dominio))
            {
                List <AD> usuarios = new List <AD>();

                using (var searcher = new PrincipalSearcher(new UserPrincipal(pc)))
                {
                    foreach (var result in searcher.FindAll())
                    {
                        DirectoryEntry de    = result.GetUnderlyingObject() as DirectoryEntry;
                        int            flags = (int)de.Properties["userAccountControl"].Value;
                        if (!Convert.ToBoolean(flags & 0x0002))
                        {
                            string _Email;
                            if (de.Properties["mail"].Value == null)
                            {
                                _Email = "";
                            }
                            else
                            {
                                _Email = de.Properties["mail"].Value.ToString();
                            }

                            string _Empresa;
                            if (de.Properties["company"].Value == null)
                            {
                                _Empresa = "";
                            }
                            else
                            {
                                _Empresa = de.Properties["company"].Value.ToString();
                            }

                            string _Departamento;
                            if (de.Properties["department"].Value == null)
                            {
                                _Departamento = "";
                            }
                            else
                            {
                                _Departamento = de.Properties["department"].Value.ToString();
                            }

                            string _Cargo;
                            if (de.Properties["title"].Value == null)
                            {
                                _Cargo = "";
                            }
                            else
                            {
                                _Cargo = de.Properties["title"].Value.ToString();
                            }

                            AD ad = new AD()
                            {
                                Nome         = de.Properties["name"].Value.ToString(),
                                Login        = de.Properties["sAMAccountName"].Value.ToString(),
                                Email        = _Email,
                                Empresa      = _Empresa,
                                Departamento = _Departamento,
                                Cargo        = _Cargo,
                            };
                            usuarios.Add(ad);
                        }
                    }
                }
                return(usuarios);
            }
        }