GroupPrincipal.IsMemberOf always returns false
I have a function that checks if a group is a member of a group. I have 2 options for a function, don't 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 they always return false. When it's time to call the GroupPrincipal.save method, there is a problem with an already existing object.
I ran a foreach loop to get the names of the members of the parent group, and compared to the new member name to be added, and there is no doubt that the member exists.
I could use LINQ to compare strings by name, but that's not ideal.
What? If I am doing something wrong? Is there a better way to determine if a group exists in a group.
Using framework 3.5 - thanks in advance
source to share
Hope this helps the next developer with the same problem:
Solved it this way:
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.
source to share