I have a c#
application which uses Active Directory
. I need to take all users which has at least a group starting with a specified value.
Here is my code:
using (PrincipalContext pcontext = new PrincipalContext(ContextType.Domain, "my-domain"))
{
UserPrincipal uspPrincipal = new UserPrincipal(pcontext);
// Here I think is the problem
uspPrincipal.GetGroups().Any(x => x.Name.StartsWith("my value"));
PrincipalSearcher ps = new PrincipalSearcher(uspPrincipal);
var results = ps.FindAll();
var resMembers = results.Where(x => x is UserPrincipal)
.Cast<UserPrincipal>()
.Distinct()
.Select(x => x.GivenName + ", " + x.Surname).ToList();
}
I receive a lot of users, but after I checked in Active Directory
I found out that the number is bigger than expected result.
I know that I can bring firstly the groups that starts with seached value and then I can take for each group it's users but this means a lot of queries in Active Directory
and I want to bring my users using just one interogation in Active Directory
, so can I obtain neded users with a single search?