locked
Active directory: members groups RRS feed

  • Question

  • Hello everybody, I am working on a method in C# (VS2005) that given a username return the group membership.

    First i make a query to the active directory (using ..sAMAccountName = *#username#*), this returns me all the users available in the active directory 
    After this i make another query to obtain the groups. I am trying to look at the groups returned and do an query with the username and other clause, maybe "ismember" or "memberof".
    I would like to know the group of the user, at first i thought that this would be a property of the user, but i cant find it. its really a property? o how can i know the group of one user?
    Actually I am a little lost. Anybody could tell the way to do it?

    this is the code:

      
    Code Snippet

          private void button1_Click(object sender, EventArgs e)
            {
                listBox1.Items.Clear();
                string Usuario = textBox1.Text;
                raiz = new DirectoryEntry();
                DirectorySearcher busqueda = new DirectorySearcher(raiz);
                busqueda.SearchRoot = raiz;
                busqueda.Filter = filtroUsu(textBox1.Text);
                busqueda.PropertyNamesOnly = true;
                busqueda.PropertiesToLoad.Add("sAMAccountName");
                SearchResultCollection resultado = busqueda.FindAll();
                foreach (SearchResult nodo in resultado)
                {
                    DirectoryEntry entrada = nodo.GetDirectoryEntry();
                    listBox1.Items.Add("Buscando en: " + raiz.Name);
                    listBox1.Items.Add("----------------------------------------------------");
                    listBox1.Items.Add(nodo.GetDirectoryEntry());

                    busqueda.Filter("(objectCategory=group)");
                    busqueda.PropertiesToLoad.Add("name");
                    SearchResultCollection Grupos = busqueda.FindAll();
                    foreach (SearchResult Grupo in Grupos)
                    {
                        DirectoryEntry entrada2 = Grupo.GetDirectoryEntry();
                               
                    }
       
                }
            }
       
            private string filtroUsu(string usuario) {
                if ((usuario == "") || (usuario == "*")) return "(sAMAccountName=*)";
                else return "(sAMAccountName=*" + usuario + "*)";                   
            }
        }




    help please!
    Tuesday, April 8, 2008 2:32 PM

Answers

All replies

  • Here is the solution. Thanks you very much


    Code Snippet

            private void button1_Click(object sender, EventArgs e)
            {
                listBox1.Items.Clear();
                string Usuario = textBox1.Text;
                raiz = new DirectoryEntry();
                DirectorySearcher busqueda = new DirectorySearcher(raiz);
                busqueda.SearchRoot = raiz;
                busqueda.Filter = filtroUsu(textBox1.Text);
                busqueda.PropertiesToLoad.Add("sAMAccountName");
                busqueda.PropertiesToLoad.Add("MemberOf");
                busqueda.PropertiesToLoad.Add("groupName");
                SearchResultCollection resultado = busqueda.FindAll();
                foreach (SearchResult nodo in resultado)
                {
                    nGrupos = nodo.Properties["memberOf"].Count;
                    DirectoryEntry entrada = nodo.GetDirectoryEntry();
                    listBox1.Items.Add("Searching in: " + raiz.Name);
                    listBox1.Items.Add("----------------------------------------------------");
                    listBox1.Items.Add(nodo.GetDirectoryEntry());
                    listBox1.Items.Add("numbers of groups: " + nGrupos);
                    string dn;
                    for (int i = 0; i < nGrupos; i++)
                    {
                        dn = (string)nodo.Properties["memberOf"][i];
                        dn = dn.Remove(dn.IndexOf(",")); 
                        dn = dn.Replace("CN=", "");
                        listBox1.Items.Add("Group " + (i + 1) + ": " + dn);
                    }
                    generaXml(Usuario);
                }
            }

            private string filtroUsu(string usuario)
            {
                if ((usuario == "") || (usuario == "*")) return "(sAMAccountName=*)";
                else return "(sAMAccountName=*" + usuario + "*)";
                //consult (&(sAMAccountName = #username#))
            }



    Friday, April 11, 2008 8:26 AM