ASP.NET如何获取Active Directory中的组列表

时间:2008-11-27 11:39:20

标签: c# asp.net vb.net active-directory

如何在Active Directory中获取完整的组列表?

2 个答案:

答案 0 :(得分:33)

查看System.DirectoryServices(ASP.NET 2.0参考):

C#-example获取群组:

using System.DirectoryServices; 

public class test
{

    private void main()
    {
        foreach (string @group in GetGroups())
        {
            Debug.Print(@group);
        }
    }

    public List<string> GetGroups()
    {
        DirectoryEntry objADAM = default(DirectoryEntry);
        // Binding object. 
        DirectoryEntry objGroupEntry = default(DirectoryEntry);
        // Group Results. 
        DirectorySearcher objSearchADAM = default(DirectorySearcher);
        // Search object. 
        SearchResultCollection objSearchResults = default(SearchResultCollection);
        // Results collection. 
        string strPath = null;
        // Binding path. 
        List<string> result = new List<string>();

        // Construct the binding string. 
        strPath = "LDAP://stefanserver.stefannet.local";
        //Change to your ADserver 

        // Get the AD LDS object. 
        try
        {
            objADAM = new DirectoryEntry(strPath);
            objADAM.RefreshCache();
        }
        catch (Exception e)
        {
            throw e;
        }

        // Get search object, specify filter and scope, 
        // perform search. 
        try
        {
            objSearchADAM = new DirectorySearcher(objADAM);
            objSearchADAM.Filter = "(&(objectClass=group))";
            objSearchADAM.SearchScope = SearchScope.Subtree;
            objSearchResults = objSearchADAM.FindAll();
        }
        catch (Exception e)
        {
            throw e;
        }

        // Enumerate groups 
        try
        {
            if (objSearchResults.Count != 0)
            {
                foreach (SearchResult objResult in objSearchResults)
                {
                    objGroupEntry = objResult.GetDirectoryEntry();
                    result.Add(objGroupEntry.Name);
                }
            }
            else
            {
                throw new Exception("No groups found");
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }

        return result;
    }

}

获取组的VB示例:

Imports System.DirectoryServices

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each group As String In GetGroups()
        Debug.Print(group)
    Next
End Sub

Public Function GetGroups() As List(Of String)
    Dim objADAM As DirectoryEntry                   ' Binding object.
    Dim objGroupEntry As DirectoryEntry             ' Group Results.
    Dim objSearchADAM As DirectorySearcher          ' Search object.
    Dim objSearchResults As SearchResultCollection  ' Results collection.
    Dim strPath As String                           ' Binding path.
    Dim result As New List(Of String)

    ' Construct the binding string.        
    strPath = "LDAP://stefanserver.stefannet.local" 'Change to your ADserver

    ' Get the AD LDS object.
    Try
        objADAM = New DirectoryEntry(strPath)
        objADAM.RefreshCache()
    Catch e As Exception
        Throw e
    End Try

    ' Get search object, specify filter and scope,
    ' perform search.
    Try
        objSearchADAM = New DirectorySearcher(objADAM)
        objSearchADAM.Filter = "(&(objectClass=group))"
        objSearchADAM.SearchScope = SearchScope.Subtree
        objSearchResults = objSearchADAM.FindAll()
    Catch e As Exception
        Throw e
    End Try

    ' Enumerate groups
    Try
        If objSearchResults.Count <> 0 Then
            Dim objResult As SearchResult
            For Each objResult In objSearchResults
                objGroupEntry = objResult.GetDirectoryEntry
                result.Add(objGroupEntry.Name)
            Next objResult
        Else
            Throw New Exception("No groups found")
        End If
    Catch e As Exception
        Throw New Exception(e.Message)
    End Try

    Return result
End Function
End Class

答案 1 :(得分:1)

Microsoft .NET Framework提供了一个标准库,用于在Active.DirectoryServices.dll中使用Active Directory: System.DirectoryServices namespace

Microsoft建议使用System.DirectoryServices命名空间中的两个主要类: DirectoryEntry DirectorySearcher 。在大多数情况下,仅使用DirectorySearcher类就足够了。

您可以在此CodeProject article中找到一些示例。

相关问题