Solved

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

Posted on 2011-02-21
14
480 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 54
XAML: Layout 8 25
Device Location 4 19
open System.IO.Compression.ZipArchiveEntry as ZipFile without storing to file 2 13
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

862 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

23 Experts available now in Live!

Get 1:1 Help Now