Get all members Email,Name of an Active Directory Group using ArrayList: C#.Net


Get members of an Active Directory Group using C#.Net

Note: To add or read properties from an Active Directory group you need to have special username and password like an Active Directory admin.

protected ArrayList GetMembersFromADGroup(string strRole)
    {      
        ArrayList membersEmail = new ArrayList();
        ArrayList membersName = new ArrayList();
        ArrayList strloginName = new ArrayList();
        string AdGroupForSysAdmin = "";        string AdGroupFolder = "";
        char[] charsToTrim = { ' ' };       

        string folderPath = "";
        folderPath = "LDAP://CN=" + AdGroupName;
        folderPath = folderPath.TrimEnd(charsToTrim) + ","; ;
        folderPath += "OU=" + AdFolderName + ",OU=Groups,DC=aaaaaa,DC=com";

// create object for the shared folder in AD using admin username and password. 
        DirectoryEntry addGroup;
        addGroup = new DirectoryEntry(folderPath, "Spluser""password");
        if (chkGroupExistence(addGroup) == true)
        {
           object allMembers = addGroup.Invoke("members"null);
           foreach (object member in (IEnumerable)allMembers)
           {
              DirectoryEntry memberEntry = new DirectoryEntry(member);
              string Email = (memberEntry.Properties["mail"].Value.ToString());
              membersEmail.Add(Email);
              string Name = (memberEntry.Properties["DisplayName"].Value.ToString());
              membersName.Add(SysAdminName);
              string adminID = memberEntry.Properties["Name"].Value.ToString();
              string adminLogin = "PAR-1\\" + adminID.ToLower();
              strloginName.Add(adminLogin);
            } 
       }
        return membersEmail;// Return Name or Email.
}

Leave your comments below.

No comments: