Solved

How to retrieve data from Acive Directory thru C# Class Library

Posted on 2011-02-21
14
483 Views
Last Modified: 2012-05-11
Hi,

Am trying to write a code to pull User Information (like "first name,last name,email address..etc)from Active Directory thru C# Class library.

Some one please help to enlighten me to complete the process,am pretty much new.
0
Comment
Question by:Sha1395
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 16

Assisted Solution

by:Kalpesh Chhatrala
Kalpesh Chhatrala earned 100 total points
ID: 34941520
0
 
LVL 10

Assisted Solution

by:Asim Nazir
Asim Nazir earned 100 total points
ID: 34941531
Hi,

Follow the steps mentioned in http://www.codeproject.com/KB/system/getuserfrmactdircsharp.aspx

Thats it!
0
 

Author Comment

by:Sha1395
ID: 34941546
Hi asimnazir,

Thanks for your quick response,even i seen that example in code project,but am not able to consolidate the whole code,i would appreciate if you give the code.

Am damn new to .Net ,so i couldn't able to figure my mistakes.That's the reason am asking sorry about that.I would be really thank full if you give me the consolidate code,where i can copy or add in my project and run straight a way.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 300 total points
ID: 34941552
Soem usefull information:
Howto: (Almost) Everything In Active Directory via C#
http://www.codeproject.com/KB/system/everythingInAD.aspx
0
 

Author Comment

by:Sha1395
ID: 34941560
Again i have to ask to create a class library (c#) to pull information from AD ,so how can i insert the code,even i follow step by step ,it shows hell out of error in my code.

Do i need to create a console application ?
0
 

Author Comment

by:Sha1395
ID: 34941578
Hi Dhaest,

Thanks for your reply,i been to code project and seen all those example before i come here.

Now i have a problem .

1) do i need to create Console Application or Class library file to use those codes ?

I was trying to create class library file and insert those codes and it ask me to create exe file.

Please suggest how and what i suppose to do,believe me am totally come from Non IT .

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 34941587
What exactly do you want to do with the results ? Display them on the screen (use a windows-application), output them to a textfile (use a console-application), ...
0
 

Author Comment

by:Sha1395
ID: 34941601
Hi Dhaest,

The Scenario for me is, i will get a string (Eg: Name : "David"),i will pass this string in to the class library(c#) and pull the information ("first name,last name,email id,etc..)from Active Directory and update the Database.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 34941611
Do you want to perform it more than once, how will you enter the name, ...
0
 

Author Comment

by:Sha1395
ID: 34941634
Right now my plan is create window application, and call this dll (class library) file inside the application,pass the string what ever enters in windows application and retrieve data from AD and update the data base.

This is the requirement i got it,they didn't accept write the whole code in windows application,the asked me to write class library file and use those dll in winforms.
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 300 total points
ID: 34941660
Just create a windows-application. Add a new class to it, fe ADSearcher (or something). Add all the methods and procedures to query the AD and give the results back to the screen (or call another method that will perform the update-query).

You can also create a dll-project, so that you compile your project and you'll need to use it from fe a windows application or console
0
 

Author Comment

by:Sha1395
ID: 34941671
Hi Dhaest,

Thanks for your reply,if you enlighten with some sample code,i will follow and develop the rest.

Please throw some help
0
 

Author Comment

by:Sha1395
ID: 34942024
Hi Guru's

After heavy google,came across this code,please suggest your opinion.

using System;
using System.Text;
using System.DirectoryServices;

namespace activeDirectoryLdapExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter user: ");
            String username = Console.ReadLine();

            try
            {
                // create LDAP connection object

                DirectoryEntry myLdapConnection = createDirectoryEntry();

                // create search object which operates on LDAP connection object
                // and set search object to only find the user specified

                DirectorySearcher search = new DirectorySearcher(myLdapConnection);
                search.Filter = "(cn=" + username + ")";

                // create results objects from search object

                SearchResult result = search.FindOne();
               
                if (result != null)
                {
                    // user exists, cycle through LDAP fields (cn, telephonenumber etc.)

                    ResultPropertyCollection fields = result.Properties;

                    foreach (String ldapField in fields.PropertyNames)
                    {
                        // cycle through objects in each field e.g. group membership
                        // (for many fields there will only be one object such as name)

                        foreach (Object myCollection in fields[ldapField])
                            Console.WriteLine(String.Format("{0,-20} : {1}", ldapField, myCollection.ToString()));
                               
                    }
                }

                else
                {
                    // user does not exist
                    Console.WriteLine("User not found!");
                }
            }

            catch (Exception e)
            {
                Console.WriteLine("Exception caught:\n\n" + e.ToString());
            }
        }

        static DirectoryEntry createDirectoryEntry()
        {
            // create and return new LDAP connection with desired settings

            DirectoryEntry ldapConnection     = new DirectoryEntry("rizzo.leeds-art.ac.uk");
            ldapConnection.Path               = "LDAP://OU=staffusers,DC=leeds-art,DC=ac,DC=uk";
            ldapConnection.AuthenticationType = AuthenticationTypes.Secure;

            return ldapConnection;
        }
    }
}
0
 

Author Comment

by:Sha1395
ID: 34942144
Any Suggestion about the code ?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question