return not breaking loop (c#)

Posted by David Wick on Stack Overflow See other posts from Stack Overflow or by David Wick
Published on 2010-06-14T17:20:43Z Indexed on 2010/06/14 17:22 UTC
Read the original article Hit count: 116

Filed under:

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;"

© Stack Overflow or respective owner

Related posts about c#