views:

467

answers:

1

I just wonder if anyone knows or made a wrapper around Active Directory to be able to easily query it in .net? Kind of like "LINQ-to-ActiveDirectory" or some SQL Dialect, i.e. to be able to do "SELECT DISTINCT(DEPARTMENT) FROM /Users/SomeOU/AnotherOU" or "SELECT user FROM domain" or whatever.

As far as I know, it is possible to query WMI and IIS in a "SQLesque" way, i just wonder if something similar is possible for Active Directory as well, without having to learn yet another Query Language (LDAP)?

+9  A: 

LINQ to Active Directory implements a custom LINQ query provider that allows querying objects in Active Directory. Internally, queries are translated into LDAP filters which are sent to the server using the System.DirectoryServices .NET Framework library.

http://www.codeplex.com/LINQtoAD

Sample (from the site):

// NOTE: Entity type definition "User" omitted in sample - see samples in release.

var users = new DirectorySource<User>(ROOT, SearchScope.Subtree);
users.Log = Console.Out;

var res = from usr in users
          where usr.FirstName.StartsWith("B") && usr.Office == "2525"
          select new { Name = usr.FirstName + " " + usr.LastName, usr.Office, usr.LogonCount };

foreach (var u in res)
{
    Console.WriteLine(u);
    u.Office = "5252";
    u.SetPassword(pwd);
}

users.Update();
Espo
Now I feel stupid, could not find that in Google earlier :-) Thanks!
Michael Stum
That happens to everyone once in i while. Next time someone needs this, they will find this stackoverflow-post instead.
Espo
+1 Great tool introduced there. Thanks!
Will Marcouiller