Solved

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

Posted on 2011-02-21
14
484 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
Industry Leaders: 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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