Hi I am using the following code to retrieve all the members of a specific active directory group. The code works but the problem is that the code returns all users in "distinguishedName" form which has a lot of extra info I don't need. I need the users to be displayed in "sAMAccountName" form. I have tried a lot of different search filters but none will display any information.

public ArrayList GetADGroupUsers()
        {
           // DataSet dset = new DataSet();
            //DataTable dt = new DataTable();
           // DataColumn dc = new DataColumn("Members");
            //DataColumn dc1 = new DataColumn("CN");
           // dt.Columns.Add(dc);
           // dt.Columns.Add(dc1);
            string group = "share_IT";
            DirectoryEntry de = GetDirectoryObject();

            SearchResult result;
            DirectorySearcher search = new DirectorySearcher();
            search.Filter = String.Format("(cn={0})", group);
            //  search.Filter = "(&(objectClass=group)(sn=" + group + "))";


            //   search.PropertiesToLoad.Add("cn");
            search.SearchRoot = de;
            result = search.FindOne();

            ArrayList userNames = new ArrayList();
            if (result != null)
            {
                for (int counter = 0; counter <
                         result.Properties["member"].Count; counter++)
                {

                    string user = (string)result.Properties["member"][counter];

                    userNames.Add(user);
                }
            }


            return userNames;
        }

Recommended Answers

All 4 Replies

The question is that you are retrieving the member info from the group, not the user it self.

Using the member info, you need to get a new directory entry corresponding to the member info returned.

If the new directory entry is of type user, you'll have all the user related info. The parameter 'SAMAccountName' will return an array of values with one item (0) having the NT account (or can be empty).

Be aware that the resulting directory entry also can be a group (nested), or machine (PC, Server), or printer, or mailbox, or whatelse directory object, so you must test this to skip (or call recursively) this entry info.

Hope this helps

Try this:

Dim GroupSearcher As New DirectorySearcher
'Change the OU path, domain and domain admin details
Dim GroupSearchRoot As New DirectoryEntry("LDAP://OU=YourGroupsOU,DC=yourdomainname,DC=com", "Your_Domain_Admin", "Admin_Password")

With GroupSearcher   
       .SearchRoot = GroupSearchRoot     
       .Filter = "(&(ObjectClass=Group)(CN=YourGroupName))"  '<<< Change the Group name here
End With

Dim Members As Object = GroupSearcher.FindOne.GetDirectoryEntry.Invoke("Members", Nothing) '<<< Get members
For Each Member As Object In CType(Members, IEnumerable)  '<<< loop through members
  Dim CurrentMember As New DirectoryEntry(Member) '<<< Get directoryentry for user
  ListBox1.Items.Add(CurrentMember.Name.Remove(0, 3))  '<<< Add user's CN(common name) to listbox
Next

Piyush

Be aware that the resulting directory entry also can be a group (nested), or machine (PC, Server), or printer, or mailbox, or whatelse directory object, so you must test this to skip the undesired entries.

Hope this helps

Following code returns the login name when you call it with fullName as a parameter.

public string getLoginName(string fullName)
        { 
            string loginName=string.Empty;
            try
                {
                    DirectorySearcher dsLogin = new DirectorySearcher();
                    dsLogin.Filter = "displayname=" + fullName;
                    SearchResult result = dsLogin.FindOne();
                    DirectoryEntry userInfo = result.GetDirectoryEntry();
                    //getting user name
                    loginName = (string)userInfo.Properties["samaccountname"].Value ?? string.Empty;
                    userInfo.Close();
                }
                catch(Exception e)
                {
                    sb.Append("Exception in loginNamefinder: " + e.Message +   Environment.NewLine);
                    writeToFile(sb);
                }
           return loginName;
        }

I followed the instruction Here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.