return not breaking loop (c#)
- by David Wick
I'm trying to determine if a user is a member of a group or not in AD. However, the following doesn't seem to be working for some reason...
public bool MemberOf(string sObjectName, string sGroup, bool bIsGroup)
{
DirectoryEntry dEntry = CreateDirectoryEntry();
DirectorySearcher dSearcher = new DirectorySearcher(dEntry);
if (bIsGroup) dSearcher.Filter = "(distinguishedName=" + sObjectName + ")";
else dSearcher.Filter = "(&(sAMAccountName=" + sObjectName + ")(objectClass=user))";
SearchResult sResult = dSearcher.FindOne();
if (sResult != null)
{
foreach (object oGroup in sResult.Properties["MemberOf"])
{
if (oGroup.ToString() == sGroup) return true;
else this.MemberOf(oGroup.ToString(), sGroup, true);
}
}
return false;
}
Another variation: http://users.business.uconn.edu/dwick/work/wtf/6-14-2010%201-15-15%20PM.png
Doesn't work either. This seems like a really dumb question... but shouldn't it break the loop upon "return true;"