Solved

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

Posted on 2011-02-21
14
479 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now