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
Solved

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

Posted on 2011-02-21
14
482 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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