Searching for User Objects C# Help

In this section, you build a Windows Forms application called UserSearch. This application is flexible insofar as a specific domain controller, username, and password to access Active Directory can be entered; otherwise, the user of the running process is used. In this application, you access the schema of the Active Directory service to get the properties of a user object. The. user can enter a filter string to head all user objects of a domain. It’s also possible to set the properties of the user objects that should be

User Interface

The user interface shows numbered steps to indicate how to use the application (see Figure 46-10):

  1. In the first step, Username, Password, and the Domain Controller can be entered. All this information is optional. If no domain controller is entered, the connection works with serverless binding. If the usemame is missing, the security context of the current user is taken.
  2. A button allows all the property names of the user object to be loaded dynamically in the listBoxProperties list box.
  3. After the property names are loaded, the properties to be displayed can be selected. The SelectionMode of the list box is set to MultiSimple.
  4. The filter to limit the search can be entered. The default value set in this dialog box searches for all user objects: (objectClass=user).
  5. Now the search can start.

Get the Schema Naming Context

This application has only two handler methods: one method for the button to load the properties and one to start the search in the domain. First, you read the properties of the user class dynamically from the schema to display it in the user interface.

In the handler buttonLoadProperties_Click() method, SetLogonlnformation() reads the username, password, and host name from the dialog box and stores them in members of the class. Next, the method SetNamingContext() sets the LOAP name of the schema and the LOAP name of the default context. ‘This Schema LOAP name is used in the call to set the properties in the list box: SetUserProperties(). .

sa

In the helper method SetNarningContext(), you are using the root of the directory root get the properties of the server. You are interested in the value of only two properties: schemaNamingContext and defaultNamingContext.

Capture

Get the Property Names of the User Class

You have the LDAP name to access the schema. You can use this to access the directory and read the properties. You are interested in not only the properties of the user class,but also those of the base classes of user: Organizational-Person, Person, and Top. In this program, the names of the base classes are hard-coded. You could also read the base class dynamically with the subClassOf attribute.

GetSchemaProperties() returns IEnumerable<string> with all property names of the specific object type. All the property names are added to the listbox:

Capture

In GetSchemaProperties(), you are accessing the Active Directory service again. This time, root DSE isnot used but rather the LDAP name to the schema that you discovered earlier.The property systemMaycontain holds a collectionof all attributes that are allowed in the class objectType:

Capture

Step 2 in the application is completed. The ListBox control has all the property names of the user objects.

Search for User Objects

The handler for the search button calls only the helper method FillResult();

Capture

In FillResult(), you do a normal search in the complete Active Directory Domain as you saw earlier. SearchScope is set to Subtree, the Filter to the string you get from a TextBox object, and the properties that should be loaded into the cache are set by the values the user selected in the list box. The PropertiesToLoad property of the DirectorySearcher is of type StringCollection where the properties that should be loaded can be added using the AddRange() method that requires a string array. The properties that should be loaded are read from the ListBox listBoxProperties with the property SelectedItems. After setting the properties of the DirectorySearcher object, the properties are searched by calling the SearchAll() method. The result of the search inside the SearchResultCollection is used to generate summary information that is written to the text box textBoxResults:

Capture

Starting the application gives you a list of all objects where the filter is valid (see Figure 46-11).

Figure 46-11

Figure 46-11

Posted on November 2, 2015 in Directory Services

Share the Story

Back to Top