CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Active Directories in .Net

By Satheesh Babu
Posted On Feb 24,2008
Article Rating:
Be first to rate
this article.
No of Comments: 1
Category:
Print this article.

Active Directories in .Net

 

Active directories are similar to databases that are used in windows environment to store information’s about networks, users, domains and assets like printers, etc. Like database, we can query and update active directory for business purposes. With this information, we will move forward and see how active directories are accessed and updated in .net with a simple example. Throughout this article I use LDAP to connect to active directory.

 

How to access AD through .Net?

.Netframework has packed set of classes in System.DirectoryServices namespace which facilitates us to gain access to AD. We have to add reference to this namespace before proceeding it further.

 

In the next section, we will see how to retrieve the whole information about a user in AD with the help of System.DirectoryServices namespace.

 

Get user information from AD:

The below code will help us to retrieve all the information of a particular user by giving his/her userid. For executing this code you need to change the below LDAP string according to your infrastructure name.

 

        DirectoryEntry dir = new DirectoryEntry();

        dir.Path = "LDAP://YourDomainController ";        

        DirectorySearcher sea = new DirectorySearcher(dir);

        sea.Filter = "(sAMAccountName=satheesb)";

        SearchResult seares = sea.FindOne();      

        StringBuilder str = new StringBuilder();

        System.DirectoryServices.ResultPropertyCollection prop = seares.Properties;

        ICollection coll = prop.PropertyNames;

        IEnumerator enu = coll.GetEnumerator(); 

            while (enu.MoveNext())

            {

                str.Append(enu.Current + " = " + seares.Properties[enu.Current.ToString()][0] + "\n");

            }

           txtSummary.Text = str.ToString();

 

If you see the above code, we are restricting the search by giving a filter to retrieve only a particular user, “satheesb” in our case. We can extend this query and narrow down the search according to our need. Multiple conditions can be given in the format,

(&(Attribute1=value)(Attribute2=value)(Attribute2=value))

The above query uses AND condition which indicates that all the conditions should be satisfied. We can use OR with the help of “|” symbol and “!” for NOT.




Update Active Directory in .Net:

We will see a simple example that will update the AD in this section. For making the understanding simple, we will search for user with surname “babu” and update their country to India. Updations to a attribute in AD can be done through DirectoryEntry class by,

DirectoryEntry.Property[“PropertyName”].Value=”new Value”;

For example:

                dir.Properties["mobile"].Value = "9901999337";

 

Implementation:

 

    try

        {

            DirectoryEntry dir = new DirectoryEntry();

            dir.Path = "LDAP://YourDomainController ";        

            DirectorySearcher sea = new DirectorySearcher(dir);

            sea.Filter = "(sn=Babu)";

            SearchResultCollection seacoll = sea.FindAll();

            Response.Write(seacoll.Count.ToString());

            StringBuilder str = new StringBuilder();

            for (int i = 0; i < seacoll.Count; i++)

            {

                DirectoryEntry d = new DirectoryEntry();

                d.Path = seacoll[i].Path;

 

                d.Properties["co"].Value = "India";

 

                d.CommitChanges();

                if (d.Properties.Contains("sAMAccountName"))

                {

                    str.Append("sAMAccountName =" + d.Properties["sAMAccountName"].Value + "\n");

                }

                if (d.Properties.Contains("co"))

                {

                    str.Append("Country =" + d.Properties["co"].Value + "\n");                   

                }

                str.Append("---------" + "\n");

            }

            txtSummary.Text = str.ToString();

            Response.Write("Completed with " + seacoll.Count.ToString() + " Row(s)");

        }

        catch (Exception ex)

        {

            Response.Write(ex.Message);

        }

For security purposes not every user id in an enterprise will have the rights to update AD. So, we can update the AD through a service account that has the access to update the AD. DirectoryEntry class will have UserName and Password property to get the service account informations.

 

dir.Username = txtServiceUID.Text;

dir.Password = txtServicePwd.Text;

 

After updating, make sure you are calling CommitChanges() method which saves the updated data to the underlying store. Note: Since, AD is very slow for data access we have to narrow down our search query as much as possible for a quicker access. We can get the entire attribute names in AD using the code listed in “Get user information from AD” section. Download the source code attached with article and change the LDAP string to see AD in action.

Download Source:

Download source
Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
dfgfd
dgdfgdfg