how to retrieve data from active directory using c#

how to retrieve data from active directory using c#:


step 1:
First include this namespace  using System.DirectoryServices;

step 2:
And in page load function use the following code
(Use your active directory domain name,username,password in the following code)

 protected void Page_Load(object sender, EventArgs e)
        {
            string domainname = "mydomain"; string username = "admin"; string password = "1234";

            DirectoryEntry entry = new DirectoryEntry("LDAP://" + domainname, username, password,    AuthenticationTypes.Secure);
            DirectorySearcher search = new DirectorySearcher(entry);
            search.Filter = "(SAMAccountName=" + username + ")"; //This is your login name(it filter based on
                                                                                                                            //your login user name)
            SearchResult results = search.FindOne();
            if (results != null)
            {
                try
                {
                    lbldisplayname.Text = results.Properties["displayName"][0].ToString(); //htis is your full name
                    lblemail.Text = results.Properties["mail"][0].ToString();   //this is your mail address
                }
                catch (Exception ex)
                {
                    lblerr.Text = ex.ToString();
                }
            }
        }


pls refer this link for any doubts  http://www.willasrari.com/blog/query-active-directory-users-using-c/000133.aspx

Comments

Popular posts from this blog

Upload Single/Multiple file by using the REST API and jQuery SharePoint 2013

A type named 'SP.Data. could not be resolved by the model error

Add content type to SharePoint List/Library using REST API