Reputation: 2915
I'm trying to understand Active Directory as quickly as I can, but so far I haven't been very successful. I have this code that returns all the users from AD which somebody else wrote and I'm supposed to alter it so that it only returns the users for a specified group. I've tried to learn a little bit more about AD in the hopes that the solution will present itself, but no luck so far. So maybe someone can help me out? Here's the code that I have.
adSearch.Filter = "(&(objectClass=user))";
string groupName = System.Configuration.ConfigurationManager.AppSettings["ADGroupName"];
string domain = adSearch.SearchRoot.Properties["dc"].Value.ToString();
DomainLabel.Text = domain + " accounts:";
foreach (SearchResult sResultSet in adSearch.FindAll())
{
if (!GetProperty(sResultSet, "givenName").Equals("") && !GetProperty(sResultSet, "sn").Equals(""))
{
string userAccountControl = GetProperty(sResultSet, "useraccountcontrol");
bool x = userAccountControl.Equals("512") || userAccountControl.Equals("66048");
if (x)
{
ListItem tempItem = new ListItem();
unsortedList.Add(GetProperty(sResultSet, "givenName") + " " + GetProperty(sResultSet, "sn"));
tempItem.Text = GetProperty(sResultSet, "givenName") + " " + GetProperty(sResultSet, "sn");
tempItem.Value = GetProperty(sResultSet, "sAMAccountName");
values.Add(tempItem);
}
}
}
Upvotes: 1
Views: 295
Reputation: 5442
I have done this in this way Where it checks the username if its in the SecurityGroup. You can Get the Users in the ActiveDirectory and then check it like this
public bool IsInSecurityGroup(string UserName)
{
bool _isInsecurityGroup;
string GroupName ="GroupName";
System.Security.Principal.WindowsIdentity MyIdentity =
System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal MyPrincipal = new
System.Security.Principal.WindowsPrincipal(MyIdentity);
return (MyPrincipal.IsInRole(GroupName)) ? true : false;
}
For checking multiple users it should work for you Getting Users From SecurityGroup or this GroupPrincipal.GetMembers Method
Upvotes: 1