views:

7580

answers:

3

How can I get a full list of Groups in my Active Directory?

A: 

Hi Pedro.

Maybe the following page can help you:

http://www.codeproject.com/KB/system/everythingInAD.aspx

/Regards Vinblad

Vinblad
First link is dead Vinblad.
Rafe Lavelle
+1  A: 

Microsoft .NET Framework provides a standard library for working with Active Directory: System.DirectoryServices namespace in the System.DirectoryServices.dll.

Microsoft recommends using two main classes from the System.DirectoryServices namespace: DirectoryEntry and DirectorySearcher. In most cases, it is enough to use DirectorySearcher class only.

You can find some examples in this CodeProject article.

splattne
+9  A: 

Check out System.DirectoryServices:

C#-example to get groups:

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) { 
            SearchResult objResult = default(SearchResult); 
            foreach (var 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 null; 
} 
}

VB-example to get groups:

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 Nothing
End Function

End Class

Stefan
great stuff, worked like a charm for me!
Nick Josevski
Thanks Stefan for the great post...i tried using the vb code in my web application but doesnt seem to work...could you please shed some light? mcuh appreciated.._____
Mo
@mo, did you change the path/URL to match *your* AD-server? Its not a real AD-server in my example.
Stefan