久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

遞歸獲取 Active Directory 組的成員,即包括子組

Get members of an Active Directory group recursively, i.e. including subgroups(遞歸獲取 Active Directory 組的成員,即包括子組)
本文介紹了遞歸獲取 Active Directory 組的成員,即包括子組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在 Active Directory 中給定一個這樣的組:

Given a group like this in Active Directory:

MainGroup
  GroupA
    User1
    User2
  GroupB
    User3
  User4

我可以使用如下代碼輕松確定 User3 是否是 MainGroup 或其任何子組的成員:

I can easily determine if User3 is member of MainGroup or any of its subgroups with code like this:

using System;
using System.DirectoryServices;

static class Program {
    static void Main() {
        DirectoryEntry user = new DirectoryEntry("LDAP://CN=User3,DC=X,DC=y");
        string filter = "(memberOf:1.2.840.113556.1.4.1941:=CN=MainGroup,DC=X,DC=y)";
        DirectorySearcher searcher = new DirectorySearcher(user, filter);
        searcher.SearchScope = SearchScope.Subtree;
        var r = searcher.FindOne();
        bool isMember = (r != null);
    }
}

我想知道是否有類似的方法來獲取屬于某個組或其任何子組的所有用戶,即在 MainGroup 的示例中獲取 User1、User2、User3 和 User4.

I would like to know if there is a similar way to get all the users that are member of a group or any of its subgroups, i.e. in the example for MainGroup get User1, User2, User3 and User4.

獲取所有用戶的明顯方法是遞歸查詢每個子組,但我想知道是否有更簡單的方法來做到這一點.

The obvious way of getting all the users is to recursively query each subgroup, but I was wondering if there is an easier way to do it.

memberOf:1.2.840.113556.1.4.1941: 過濾器使用相同的方法,但使用域根而不是用戶作為搜索基礎(chǔ)是不可行的,因為查詢需要太多l(xiāng)ong(可能它會遞歸計算域中所有用戶的所有組成員身份,并檢查他們是否是給定組的成員).

Using the same approach with the memberOf:1.2.840.113556.1.4.1941: filter, but using the domain root instead of the user as a search base is not feasible, as the query takes too long (probably it computes all the group memberships recursively for all users in the domain and checks if they are member of the given group).

獲取群組所有成員(包括其子群組)的最佳方式是什么?

Which is the best way to get all members of a group, including its subgroups?

推薦答案

以防萬一這可能對其他人有益:這是我最終得到的解決方案.這只是一個遞歸搜索,有一些額外的檢查,以避免檢查同一個組或用戶兩次,例如如果 groupA 是 groupB 的成員并且 groupB 是 groupA 的成員或者用戶是多個組的成員.

Just in case this might benefit someone else: here is the solution I ended up with. It is just a recursive search, with some extra checks to avoid checking the same group or user twice, e.g. if groupA is member of groupB and groupB is member of groupA or a user is member of more than one group.

using System;
using System.DirectoryServices;
using System.Collections.Generic;

static class Program {

    static IEnumerable<SearchResult> GetMembers(DirectoryEntry searchRoot, string groupDn, string objectClass) {
        using (DirectorySearcher searcher = new DirectorySearcher(searchRoot)) {
            searcher.Filter = "(&(objectClass=" + objectClass + ")(memberOf=" + groupDn + "))";
            searcher.PropertiesToLoad.Clear();
            searcher.PropertiesToLoad.AddRange(new string[] { 
                "objectGUID",
                "sAMAccountName",
                "distinguishedName"});
            searcher.Sort = new SortOption("sAMAccountName", SortDirection.Ascending);
            searcher.PageSize = 1000;
            searcher.SizeLimit = 0;
            foreach (SearchResult result in searcher.FindAll()) {
                yield return result;
            }
        }
    }

    static IEnumerable<SearchResult> GetUsersRecursively(DirectoryEntry searchRoot, string groupDn) {
        List<string> searchedGroups = new List<string>();
        List<string> searchedUsers = new List<string>();
        return GetUsersRecursively(searchRoot, groupDn, searchedGroups, searchedUsers);
    }

    static IEnumerable<SearchResult> GetUsersRecursively(
        DirectoryEntry searchRoot,
        string groupDn,
        List<string> searchedGroups,
        List<string> searchedUsers) {
        foreach (var subGroup in GetMembers(searchRoot, groupDn, "group")) {
            string subGroupName = ((string)subGroup.Properties["sAMAccountName"][0]).ToUpperInvariant();
            if (searchedGroups.Contains(subGroupName)) {
                continue;
            }
            searchedGroups.Add(subGroupName);
            string subGroupDn = ((string)subGroup.Properties["distinguishedName"][0]);
            foreach (var user in GetUsersRecursively(searchRoot, subGroupDn, searchedGroups, searchedUsers)) {
                yield return user;
            }
        }
        foreach (var user in GetMembers(searchRoot, groupDn, "user")) {
            string userName = ((string)user.Properties["sAMAccountName"][0]).ToUpperInvariant();
            if (searchedUsers.Contains(userName)) {
                continue;
            }
            searchedUsers.Add(userName);
            yield return user;
        }
    }

    static void Main(string[] args) {
        using (DirectoryEntry searchRoot = new DirectoryEntry("LDAP://DC=x,DC=y")) {
            foreach (var user in GetUsersRecursively(searchRoot, "CN=MainGroup,DC=x,DC=y")) {
                Console.WriteLine((string)user.Properties["sAMAccountName"][0]);
            }
        }
    }

}

這篇關(guān)于遞歸獲取 Active Directory 組的成員,即包括子組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應(yīng)該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設(shè)置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調(diào)用)
主站蜘蛛池模板: 99reav| 自拍偷拍亚洲一区 | 精品久久久久久中文字幕 | 欧美日韩大陆 | 一区二区三区免费 | 亚洲欧美综合精品久久成人 | 国产激情一区二区三区 | 国产精品视频在线观看 | 亚洲精品一区二区三区蜜桃久 | 三级黄片毛片 | h视频在线播放 | 在线āv视频 | 久久久久国产一区二区三区 | 色在线免费视频 | 午夜影院在线观看 | 亚洲精彩视频在线观看 | 美国十次成人欧美色导视频 | 99国产精品一区二区三区 | 成人免费黄色片 | 视频一区二区三区四区五区 | 欧美日韩精品免费 | 一区二区三区视频在线观看 | 亚洲一级视频在线 | 一级午夜aaa免费看三区 | 日韩电影一区二区三区 | 久久精品高清视频 | 久久免费看 | 少妇av片 | 国产欧美在线播放 | 在线国产一区二区三区 | 国产a一区二区 | 国产精品久久久久aaaa九色 | 国产成人免费视频网站高清观看视频 | 精品欧美一区二区精品久久久 | 青草久久免费视频 | 青草福利 | 国产精品久久午夜夜伦鲁鲁 | 国产一区 在线视频 | 日韩字幕| 亚洲免费视频在线观看 | 久久综合久色欧美综合狠狠 |