LINQ to Active Directory with CodePlex

Interacting with Active Directory is done with the DirectoryEntry Class. In addition to this, if we want to interact with Active directory then we can make use of CodePlex LINQ, but this is not traditional LINQ which is provided by Microsoft.

The main advantage of this implementation is that we can use it just like normal LINQ. Codeplex LINQ to Active directory implements a custom LINQ query provider that allows querying objects in Active Directory.

It has following features – (According to http://linqtoad.codeplex.com/)

• Translates into LDAP filters
• Simple and approachable entity model with support of propagating updates back.
• Supports mappings to both the System.DirectoryServices (.NET) and ActiveDs (COM) APIs.
• Ships with a set of samples.

In above link you can find more details about this implementation.

Now check the sample code to interact with Active directory using Linq-

Getting List of Users from Active Directory

//Code to get List of Users from Active Directory using Linq
        private void btnGetUsers_Click(object sender, EventArgs e)
         {
             try
            {
                 //Connectiong to Active directory
                 DirectoryEntry ROOT = new DirectoryEntry( "AD path");
                 //Getting users from Active directory
                var users = new DirectorySource<User>(ROOT, SearchScope.Subtree);
                 var objUser = from usr in users
                               select new { usr.Name, usr.Description, usr.Groups };

               //Looping users
               List<User> lstUsers = new List<User>();
                 foreach (var u in objUser)
                 {
                     //User related property can be set here- Like Password
                     lstUsers.Add(new User { Name = u.Name, Description = u.Description });
                 }
            }
            catch (Exception  ex)
            {
               MessageBox .Show(ex.Message);
                 
             }
         }

Getting List of Groups from Active Directory

//Code to get List of groups from Active Directory using Linq
        private void btnGetGroups_Click(object sender, EventArgs e)
         {
             try
            {
                 //Connectiong to Active directory
                DirectoryEntry ROOT = new DirectoryEntry("AD path");
                 //Getting groups from Active directory
                var groups = new DirectorySource<Group>(ROOT, SearchScope.Subtree);
                groups.Log = Console.Out;

                var objGrp = (from grp in groups
                              select new { grp.Name });

                 //Looping groups
               List<Group> lstGroups = new List<Group>();
                foreach (var g in objGrp)
                 {
                     //Group related property can be set here
                     lstGroups.Add(new Group { Name = g.Name });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

In above code, you need to provide your own AD path to interact with Active directory. Here User and Group both are entity classes which contain the properties for user and group. You can find these classes in attached code.

[DirectorySchema("user", typeof(IADsUser))]
   class User
    {
         public string Name { get; set; }
        public string Description { get; set; }
        public int LogonCount { get; set; }
        [DirectoryAttribute("PasswordLastChanged", DirectoryAttributeType.ActiveDs)]
         public DateTime PasswordLastSet { get; set; }
        [DirectoryAttribute("distinguishedName")]
        public string Dn { get; set; }
        [DirectoryAttribute("memberOf")]
        public string[] Groups { get; set; }
    }

     [DirectorySchema("group")]
    class Group
    {
         public string Name { get; set; }
        [DirectoryAttribute("member")]
        public string[] Members { get; set; }
    }

You can fetch the complete code from here-

LinqToAD

By Jitendra Faye   Popularity  (3151 Views)