13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

LINQ beyond .NET 3.5347LINQ TO ACTIVE DIRECTORYAlmost all companies runn<strong>in</strong>g their IT <strong>in</strong>frastructure on W<strong>in</strong>dows use Active Directoryto manage users, computers, sett<strong>in</strong>gs, and more. Active Directory is a directoryserver that implements LDAP as one query protocol, but other LDAP servers are available,<strong>in</strong>clud<strong>in</strong>g the free OpenLDAP platform. 8Bart De Smet 9 (who now works for Microsoft) implemented a prototype LINQ toActive Directory provider as a tutorial on how LINQ providers work—but it has alsoproved to be a valuable rem<strong>in</strong>der of how broadly LINQ is targeted. Despite the name,it is capable of query<strong>in</strong>g non-Microsoft servers. He has also repeated the feat with aLINQ to SharePo<strong>in</strong>t provider, which we won’t cover here, but which is a more completeprovider implementation.If you’re not familiar with directories, you can th<strong>in</strong>k of them as a sort of cross betweenfile system directory trees and databases. They form hierarchies rather than tables, buteach node <strong>in</strong> the tree is an entity with properties and an associated schema. (For thoseof you who are familiar with directories, please forgive this gross oversimplification.)Install<strong>in</strong>g and populat<strong>in</strong>g a directory is a bit of an effort, so for the sample codeI’ve connected to a public server. If you happen to have access to an <strong>in</strong>ternal server,you’ll probably f<strong>in</strong>d the results more mean<strong>in</strong>gful if you connect to that.List<strong>in</strong>g 12.21 connects to an LDAP server and lists all the users whose first namebeg<strong>in</strong>s with K. The Person type is described elsewhere <strong>in</strong> the sample source code,complete with attributes to describe to LINQ to Active Directory how the propertieswith<strong>in</strong> the object map to attributes with<strong>in</strong> the directory.List<strong>in</strong>g 12.21LINQ to Active Directory sample, query<strong>in</strong>g users by first namestr<strong>in</strong>g url = "LDAP://ldap.andrew.cmu.edu/dc=cmu,dc=edu";DirectoryEntry root = new DirectoryEntry(url);root.AuthenticationType = AuthenticationTypes.None;var users = new DirectorySource(root, SearchScope.Subtree);users.Log = Console.Out;var query = from user <strong>in</strong> userswhere user.FirstName.StartsWith("K")select user;foreach (Person person <strong>in</strong> query){Console.WriteL<strong>in</strong>e (person.DisplayName);foreach (str<strong>in</strong>g title <strong>in</strong> person.Titles){Console.WriteL<strong>in</strong>e(" {0}", title);}}Are you gett<strong>in</strong>g a sense of déjà vu yet? List<strong>in</strong>g 12.21 shows yet another query expression,which just happens to target LDAP. If you didn’t have the first part, which sets the8www.openldap.org9http://blogs.bartdesmet.net/bartLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!