GroupPrincipal.IsMemberOf always returns false

谁都会走 提交于 2019-12-24 01:18:30

问题


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


回答1:


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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/7648887/groupprincipal-ismemberof-always-returns-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!