简体   繁体   中英

How to filter a specific group in Active Directory from asp.net c# application

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.

看完SO上的其他十二个线程后,今天早晨我偶然发现了一个回答了我问题的问题…… 获取AD组的成员

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM