locked
How to get all AD attributes listed using UserPrincipal and PrincipalContext RRS feed

  • Question

  • User-718146471 posted

    Hello folks, I have been liking what I find with using PrincipalContext and UserPrincipal. However, what I want to do is load all AD attributes into an ArrayList and feed that into a data table to populate a detailsview control. However, I am stumped; I simply cannot figure out how to make this work. I would like to pass a saMAccountName to the query, then ask AD to return all user properties and their corresponding values. Here is my code at this point.

            {
                // TODO: Add panel that displays AD Details about selected PIN
                LinkButton lnk = (LinkButton)sender;
                string domainName = ddlDomainName.SelectedValue.ToString();
    
                PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
                UserPrincipal user = UserPrincipal.FindByIdentity(ctx, lnk.Text.ToString());
                DirectoryEntry up_User = (DirectoryEntry)user.GetUnderlyingObject();
                DirectorySearcher deSearch = new DirectorySearcher(up_User);
                SearchResultCollection results = deSearch.FindAll();
                int i = 0;
                if (results != null && results.Count > 0)
                {
                    DataTable dt = new DataTable();
                    ResultPropertyCollection rpc = results[0].Properties;
                    dt.Columns.Add("PropName");
                    dt.Columns.Add("PropValue");
    
                    foreach (string rp in rpc.PropertyNames)
                    {
                    //   Property name    rp.ToString();
                    //   property value    rpc[rp][0].ToString();
                         i++;
                    }
                    dvADPinDetails.DataSource = dt;
                    dvADPinDetails.DataBind();
                    pnlADPinDetails.Visible = true;
                    pnlGroupMembers.Visible = false;
                }
            }
    

    I am thinking about using a gridview for this purpose. Any ideas?

    Tuesday, November 7, 2017 5:38 PM

Answers

  • User-718146471 posted

    Never mind, I figured it out on my own. This is what I ended up doing:

                LinkButton lnk = (LinkButton)sender;
                string domainName = ddlDomainName.SelectedValue.ToString();
    
                PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
                UserPrincipal user = UserPrincipal.FindByIdentity(ctx, lnk.Text.ToString());
                DirectoryEntry up_User = (DirectoryEntry)user.GetUnderlyingObject();
                DirectorySearcher deSearch = new DirectorySearcher(up_User);
                SearchResultCollection results = deSearch.FindAll();
                if (results != null && results.Count > 0)
                {
                    DataTable dt = new DataTable();
                    ResultPropertyCollection rpc = results[0].Properties;
                    dt.Columns.Add("PropName");
                    dt.Columns.Add("PropValue");
    
                    foreach (string rp in rpc.PropertyNames)
                    {
                        if (rpc[rp][0].ToString() == "System.Byte[]")
                        {
                            // skipping this line
                        }
                        else
                        {
                            dt.Rows.Add(rp.ToString(), rpc[rp][0].ToString());
                        }
                    }
                    gvADPinDetails.DataSource = dt;
                    gvADPinDetails.DataBind();
                    pnlADPinDetails.Visible = true;
                    pnlGroupMembers.Visible = false;
                }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 7, 2017 7:22 PM