Monday, March 18, 2013

Get users information through active directory

If you have an requirement where you have to read users information from active directory then dot net has provided a dll named "System.DirectoryServices dll ".

Get users information through active directory.

1.You need to add System.DirectoryServices dll reference in your project.

2.Add following code in default.aspx page.
        it automatically finds user name from Page.User.Identity.Name. We have added some properties like: sAMAccountName it only load those properties which we have mentioned in our code.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.DirectoryServices;
using System.Runtime.InteropServices;
using System.Security.Authentication;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        SearchResultCollection users;
        string loginUserName = "";
        if (string.IsNullOrEmpty(Page.User.Identity.Name) == false)
            loginUserName = Page.User.Identity.Name.ToString().Substring(Page.User.Identity.Name.ToString().LastIndexOf("\\") + 1);
        using (DirectoryEntry Root = new DirectoryEntry())
            //Establish connection to current loged on users Active Directory
            using (DirectorySearcher Searcher = new DirectorySearcher(Root))
                //Start at the top              
                Searcher.Filter = "(&(objectCategory=person)(anr=" + loginUserName.ToUpper().Trim() + "))";
                Searcher.SearchScope = SearchScope.Subtree;
                //Start at the top and keep drilling down
                //Load User ID
                //Load Display Name
                //Load Users first name
                //Load Users last name
                //Users Distinguished name
                //Ext. Number
                //7D Phone Number
                //Cell Phone Number
                //Cell Phone Number
                Searcher.Sort.PropertyName = "sn";
                //Sort by last name
                Searcher.Sort.Direction = System.DirectoryServices.SortDirection.Ascending;
                users = Searcher.FindAll();
                    //Users contains our searh results
                    //If it's zero then no matches were found
                    if (users.Count > 0)
                        //goes throug each user in the search results
                        foreach (SearchResult User in users)
                           // ResultPropertyCollection rcol = User.Properties;
                            ResultPropertyCollection props = User.Properties;
                            foreach (string prop in props.PropertyNames )
                                ResultPropertyValueCollection values = props[prop];
                                foreach (string val in values)
                                    //if (prop=="mail")
                                    //    Response.Write(val);
                                    Response.Write("<br>"+prop + "= " + val);
                        // Store the DataTable in ViewState.
Active directory can provide a lot of information for a user. I have collected some of information which i required for my task.

Happy Coding!!

No comments:

Post a Comment