2

I have a function that checks if a group is a member of a group. I have 2 variations of the function, neither work as expected:

public bool IsGroupGroupMember(GroupPrincipal gp, GroupPrincipal pgp)
        {
            return gp.IsMemberOf(pgp); 
        }

AND

 public bool IsGroupGroupMember(GroupPrincipal gp, GroupPrincipal pgp)
        {
            if (gp != null && pgp != null)
            {
                return pgp.Members.Contains(gp);
            }
            else
            {
                return false;
            }
        }

Both look promising, however always return false. When it comes time to call the GroupPrincipal.save method, an object already exists error is thrown.

I ran a foreach loop to get the names of the members of the parent group, and compared with the new member name to be added and there is no doubt the member does exist.

I could use LINQ to do string comparison on name, but its not ideal.

What? If anything am I doing wrong? Is there a better method to determine if a group exists in a group.

Using framework 3.5 - thanks in advance

JL.
  • 78,954
  • 126
  • 311
  • 459
  • The first method works fine for me. Con you give a bit more on the context where you call this method ? – JPBlanc Oct 04 '11 at 14:44
  • From you question I understand that the checked `GroupPrinciple` (`gp`) is a new principle to be added to a group? – Gert Arnold Oct 04 '11 at 14:57
  • @JPBlanc thanks for testing - It could be that it is comparing the groups at an object level and there is some difference at the object level, I am just not sure. – JL. Oct 04 '11 at 15:29

3 Answers3

3

I understand it's kind of late, but for future references you might want to try this.

public bool IsGroupGroupMember(GroupPrincipal gp, GroupPrincipal pgp)
    {
        return gp.GetMembers(true).Contains(pgp);
    }
Cyrus
  • 99
  • 1
  • 2
1

I hope this helps the next developer with the same issue:

Solved it like this:

public bool IsGroupGroupMember(GroupPrincipal gp, GroupPrincipal pgp)
        {
            PrincipalSearchResult<Principal> result = gp.GetGroups();
            Principal grp = result.Where(g => g.Sid == pgp.Sid).FirstOrDefault();

            if (grp == null)
            {
                return false; 
            }
            else
            {
                return true; 
            }
}

I still don't know why the methods in my original question didn't work as expected.

JL.
  • 78,954
  • 126
  • 311
  • 459
0

In my case the problem was related to the size of the group as also described here.

Mr_T
  • 193
  • 1
  • 6