Reputation: 2883
I have strange problem when querying Active Directory in C#.
var ctx = new PrincipalContext(ContextType.Domain, "adr", "usr", "pwd");
var entry = new DirectoryEntry("LDAP://" + adr, usr, pwd);
var searcher = new DirectorySearcher(entry) { Filter = "(&(sAMAccountName=user_to_search))", PageSize = 2000 };
foreach (SearchResult searchUser in searcher.FindAll())
{
// groups
var groups = searchUser.GetPropertyValues("memberof");
}
var groups = UserPrincipal.FindByIdentity(ctx, "usr_to_search").GetGroups(ctx).ToList();
But the results are not the same:
PrincipalSearcher
returns 14 groups DirectorySearcher
returns 12 groupsWell, is this bug or did I miss something?
Thanks
Upvotes: 2
Views: 1185
Reputation: 2883
Oh my god, i have mistake in my extension method ( i < prop.count - 1).
public static List<string> GetPropertyValues(this SearchResult searchResult,string property)
{
var prop = searchResult.Properties[property];
var results = new List<string>();
if (prop != null && prop.Count > 0)
{
for (int i = 0; i < prop.Count - 1; i++)
{
results.Add(prop[i].ToString());
}
}
return results;
}
Sorry for stupid question.
Upvotes: 2