15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

searching for User objects ❘ OC147<br />

classes of user: Organizational-Person, Person, <strong>and</strong> Top. In this program, the names of the<br />

base classes are hard-coded. You could also read the base class dynamically with the subClassOf<br />

attribute.<br />

GetSchemaProperties() returns IEnumerable with all property names of the specific object<br />

type. All the property names are added to the list box:<br />

private void SetUserProperties(string schemaNamingContext)<br />

{<br />

var properties = from p in GetSchemaProperties(schemaNamingContext,<br />

"User").Concat(<br />

GetSchemaProperties(schemaNamingContext,<br />

"Organizational-Person")).Concat(<br />

GetSchemaProperties(schemaNamingContext, "Top"))<br />

orderby p<br />

select p;<br />

}<br />

listBoxProperties.DataContext = properties;<br />

code snippet UserSearch/MainWindow.xaml.cs<br />

In GetSchemaProperties(), you are accessing the Active Directory service again. This time, rootDSE<br />

is not used but rather the LDAP name of the schema that you discovered earlier. The property<br />

systemMayContain holds a collection of all attributes that are allowed in the class objectType:<br />

private IEnumerable GetSchemaProperties(string schemaNamingContext,<br />

string objectType)<br />

{<br />

IEnumerable data;<br />

using (var de = new DirectoryEntry())<br />

{<br />

de.Username = username;<br />

de.Password = password;<br />

}<br />

de.Path = String.Format("LDAP://{0}CN={1},{2}", hostname, objectType,<br />

schemaNamingContext);<br />

PropertyValueCollection values = de.Properties["systemMayContain"];<br />

data = from s in values.Cast()<br />

orderby s<br />

select s;<br />

}<br />

return data;<br />

Step 2 in the application is completed. The ListBox control has all the property names of the<br />

user objects.<br />

search for user objects<br />

The h<strong>and</strong>ler for the search button calls only the helper method FillResult():<br />

private void OnSearch(object sender, RoutedEventArgs e)<br />

{<br />

try<br />

{<br />

FillResult();<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!