0

The following code works to get all of the users listed in my AD. Now I need to limit the results to just one group. How can I modify the following to get just the members of the "Everybody" group in Active Directory?

Code Behind:

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();

        dt.Columns.AddRange(new DataColumn[4]
        {
            new DataColumn("givenName", typeof (string)),
            new DataColumn("sn", typeof (string)),
            new DataColumn("mail", typeof (string)),
            new DataColumn("department", typeof (string))
        });

        using (var context = new PrincipalContext(ContextType.Domain, null))            
        {
            using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
            {
                foreach (var result in searcher.FindAll())
                {
                    DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
                    var clsuser = new ClsUsers();
                    dt.Rows.Add
                    (
                        Convert.ToString(de.Properties["givenName"].Value), 
                        Convert.ToString(de.Properties["sn"].Value),
                        Convert.ToString(de.Properties["mail"].Value),
                        Convert.ToString(de.Properties["department"].Value)
                    );
                }

                grdvList.DataSource = dt;
                grdvList.DataBind();
            }
        }
    }

Here's the gridview that it's populating:

<asp:GridView ID="grdvList" runat="server" AutoGenerateColumns="False" >
    <Columns>
        <asp:BoundField DataField="givenName" HeaderText="GivenName" ReadOnly="True" />
        <asp:BoundField DataField="sn" HeaderText="sn" ReadOnly="true" />                    
        <asp:BoundField DataField="mail" HeaderText="Email" ReadOnly="true" />
        <asp:BoundField DataField="department" HeaderText="Department" ReadOnly="true" />
    </Columns> 
</asp:GridView>

I have tried to modify the code a bit by changing the context var to specify "Everybody", but I get an error: Attempted code mod:

using (var context = new PrincipalContext(ContextType.Domain, null, "CN=Everybody", "DC=mydomain", "DC=org"))

Here's the error:

System.DirectoryServices.AccountManagement.PrincipalOperationException: 'A local error has occurred.

user1916528
  • 379
  • 4
  • 23
  • Possible duplicate of [get all users from a group in Active Directory](https://stackoverflow.com/questions/7915145/get-all-users-from-a-group-in-active-directory) – Am_I_Helpful Aug 29 '18 at 17:49

1 Answers1

0

After looking at a dozen other threads on SO, I stumbled across one this morning that answered my question ... Get Members of an AD Group .

user1916528
  • 379
  • 4
  • 23