2

ドメインを持つActiveDirectoryがあり、その下に多くのグループを含むmyDomain.localが存在します。 これらすべてのサブグループを(プログラムで)読み取って、それらの名前のリストを取得するにはどうすればよいですか? そして、クエリを最適化して結果をフィルタリングし、単語で終わるすべてのグループを取得するようにするにはどうすればよいですか? ところで、私はC#.Net、ASP.Net、SharePointを使用していますが、ADの経験はありません。Distribution Group

Region

4

2 に答える 2

3

.NET 3.5を使用している(またはアップグレードできる)場合は、System.DirectoryServices.AccountManagement名前空間を使用して次のコードを使用できます。

// create the "context" in which to operate - your domain here, 
// as the old-style NetBIOS domain, and the container where to operate in
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "cn=Distribution Group,dc=YourDomain,dc=local");

// define a "prototype" - an example of what you're searching for
// Here: just a simple GroupPrincipal - you want all groups
GroupPrincipal prototype = new GroupPrincipal(ctx);

// define a PrincipalSearcher to find those principals that match your prototype
PrincipalSearcher searcher = new PrincipalSearcher(prototype);

// define a list of strings to hold the group names        
List<string> groupNames = new List<string>();

// iterate over the result of the .FindAll() call
foreach(var gp in searcher.FindAll())
{
    // cast result to GroupPrincipal
    GroupPrincipal group = gp as GroupPrincipal;

    // if everything - grab the group's name and put it into the list
    if(group != null)
    {
       groupNames.Add(group.Name);
    }
}

それはあなたのニーズを満たしていますか?

System.DirectoryServices.AccountManagement名前空間の詳細については、MSDNマガジンの.NETFramework3.5の記事にあるディレクトリセキュリティプリンシパルの管理を参照してください。

于 2010-07-01T17:14:12.233 に答える
1

これが私が作った解決策です。興味のある方へ:

public ArrayList getGroups()
{
    // ACTIVE DIRECTORY AUTHENTICATION DATA
    string ADDomain = "myDomain.local";
    string ADBranchsOU = "Distribution Group";
    string ADUser = "Admin";
    string ADPassword = "password";

    // CREATE ACTIVE DIRECTORY ENTRY 
    DirectoryEntry ADRoot 
        = new DirectoryEntry("LDAP://OU=" + ADBranchsOU
                             + "," + getADDomainDCs(ADDomain),
                             ADUser, 
                             ADPassword);

    // CREATE ACTIVE DIRECTORY SEARCHER
    DirectorySearcher searcher = new DirectorySearcher(ADRoot);
    searcher.Filter = "(&(objectClass=group)(cn=* Region))";
    SearchResultCollection searchResults = searcher.FindAll();

    // ADDING ACTIVE DIRECTORY GROUPS TO LIST
    ArrayList list = new ArrayList();
    foreach (SearchResult result in searchResults)
    {
        string groupName = result.GetDirectoryEntry().Name.Trim().Substring(3);
        list.Add(groupName);
    }
    return list; 
}

public string getADDomainDCs(string ADDomain)
{
    return (!String.IsNullOrEmpty(ADDomain)) 
        ? "DC=" + ADDomain.Replace(".", ",DC=") 
        : ADDomain;
}
于 2010-07-03T13:32:32.390 に答える