How to get all the AD groups for a particular user?

后端 未结 10 1678
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 04:19

I checked this post already. But it doesn\'t answer my question. I want to get all the active directory groups in which a particular user is a member.

I\'ve written

10条回答
  •  忘掉有多难
    2020-11-28 05:16

    Just query the "memberOf" property and iterate though the return, example:

                search.PropertiesToLoad.Add("memberOf");
                StringBuilder groupNames = new StringBuilder(); //stuff them in | delimited
    
                    SearchResult result = search.FindOne();
                    int propertyCount = result.Properties["memberOf"].Count;
                    String dn;
                    int equalsIndex, commaIndex;
    
                    for (int propertyCounter = 0; propertyCounter < propertyCount;
                        propertyCounter++)
                    {
                        dn = (String)result.Properties["memberOf"][propertyCounter];
    
                        equalsIndex = dn.IndexOf("=", 1);
                        commaIndex = dn.IndexOf(",", 1);
                        if (-1 == equalsIndex)
                        {
                            return null;
                        }
                        groupNames.Append(dn.Substring((equalsIndex + 1),
                                    (commaIndex - equalsIndex) - 1));
                        groupNames.Append("|");
                    }
    
                return groupNames.ToString();
    

    This just stuffs the group names into the groupNames string, pipe delimited, but when you spin through you can do whatever you want with them

提交回复
热议问题