Enumerating User Memberships

This topic includes information and a code example that shows how to use a Windows form to enumerate user memberships.

To create a Windows form to display user memberships

  1. Open Visual Studio and select New Project.

  2. In the Project Types pane click Visual C# or Visual Basic, and in the Templates pane, click Windows Applications.

  3. Name the new project and click OK.

  4. Click Project>Add Reference and click System.DirectoryServices from the list displayed on the .NET tab.

  5. On the form design page, drag a text box from the Toolbox to the form and format it. This is where the user will add a user name to bind to.

  6. Drag a label from the Toolbox to the form and modify the Text property to read "Enter Name:"

  7. Drag a button from the Toolbox to the form and modify the Text property to read "Find Groups".

  8. Drag a ListBox from the Toolbox to the form. The results will be displayed in the form.

  9. Double-click the form to go to the code page.

  10. If building the C# sample, add the "using System.DirectoryServices;" statement to the end of the using statement list. If building the Visual Basic sample, add the "Imports System.DirectoryServices" statement to the end of the Imports statement list.

  11. Add the code example that follows this procedure to the source file.

  12. Compile and run the application.

The following example shows how to use a Windows form to enumerate user memberships.

Shared Sub Main() 
    Application.Run(New Form1())
End Sub 'Main

Private Sub label1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 

End Sub 'label1_Click

Private Sub textBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) 

End Sub 'textBox1_TextChanged

Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
    Dim strUserADsPath As String = "LDAP://fabrikam/cn=" + textBox1.Text + ",cn=users,dc=fabrikam,dc=com"
    Dim oUser As DirectoryEntry
    oUser = New DirectoryEntry(strUserADsPath)
    listBox1.Items.Add("Groups to which {0} belongs:" + oUser.Name)

    ' Invoke IADsUser::Groups method.
    Dim groups As Object = oUser.Invoke("Groups")
    Dim group As Object
    For Each group In CType(groups, IEnumerable)
        ' Get the Directory Entry.
        Dim groupEntry As New DirectoryEntry(group)
        listBox1.Items.Add(groupEntry.Name)
    Next group
End Sub 'button1_Click


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) 

End Sub 'Form1_Load
static void Main() 
{
    Application.Run(new Form1());
}

private void label1_Click(object sender, System.EventArgs e)
{
}

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
}

private void button1_Click(object sender, System.EventArgs e)
{
    string strUserADsPath = "LDAP://fabrikam/cn=" +textBox1.Text +",cn=users,dc=fabrikam,dc=com";
    DirectoryEntry oUser;
    oUser = new DirectoryEntry(strUserADsPath);
    listBox1.Items.Add("Groups to which {0} belongs:"+ oUser.Name);

    // Invoke IADsUser::Groups method.
    object groups = oUser.Invoke("Groups");
    foreach ( object group in (IEnumerable)groups)   
    {
        // Get the Directory Entry.
        DirectoryEntry groupEntry  = new DirectoryEntry(group);
        listBox1.Items.Add(groupEntry.Name); 
    }
}
private void Form1_Load(object sender, System.EventArgs e)
{
}

See Also

Reference

System.DirectoryServices

Concepts

User Management

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.