I am writing a C# application to display users within a user supplied security group. It seems like all my test cases work except when I search against "Domain Users". When I do neither Surname or GivenName will return values. All other groups return those values. I'm stumped.
Test 1: Domain Admins
Results: All Surname and GivenName displayed in GridView
Test 2: Domain Users
Results: No Surname or GivenName's are displayed
Note: If I change the properties to any other AD User Property, e.g., Name, SAM, etc., those all display without issue.
namespace adsearch
{
public partial class frmViewGroupMembers : Form
{
private string group;
private DataTable GroupMembers = new DataTable();
public frmViewGroupMembers()
{
InitializeComponent();
}
public void ShowGroupMembers(string groupName) {
GroupMembers.Columns.Add("Last Name");
GroupMembers.Columns.Add("First Name");
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "mydomain.local")) {
using (GroupPrincipal group = GroupPrincipal.FindByIdentity(context, groupName))
{
foreach (UserPrincipal user in group.GetMembers(true).OfType<UserPrincipal>())
{
GroupMembers.Rows.Add(user.Surname, user.GivenName);
}
}
}
dgvGroupMembers.DataSource = GroupMembers;
dgvGroupMembers.Sort(dgvGroupMembers.Columns[0], ListSortDirection.Ascending);
this.Show();
}
}
}
