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)?
You could query ADSI just by using the domain name and domain container. In this article, I am going to use the following OU and DC. Note: Please replace the OU, DC and CN information mentioned here with your company's OU, CN and DC. The first method to query Active Directory from SQL Server is by using OpenRowSet.
Active Directory: It is a container that consists of organization units for all users, their credentials, groups. All users must authenticate themselves to use an organization resource.
Open SQL Server Management Studio and connect to an instance of SQL Server. In the Object Explorer, expand the node for the SQL Server database. In the Server Objects node, right-click Linked Servers and click New Linked Server.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With