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;
        }

Edited 5 Years Ago by AlBars: n/a

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

Edited 5 Years Ago by lolafuertes: n/a

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

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post. Snipped 'fake sig' link. Please restrict such links to your site-wide user signature, which can be edited from the user control panel

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

This article has been dead for over six months. Start a new discussion instead.