开发者

GroupPrincipal.IsMemberOf always returns false

开发者 https://www.devze.com 2023-04-10 07:18 出处:网络
I have a function that checks if a group is a member of a group. I have 2 variation开发者_开发技巧s of the function, neither work as expected:

I have a function that checks if a group is a member of a group. I have 2 variation开发者_开发技巧s 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


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);
    }


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号