locked
Displaying Active directory user's information RRS feed

  • Question

  •  

    Hello evryone, I'm new to this forum and i'm looking for some help.
    I'm creating a code in C# that will look for a user in active directory and when run, it will display all the user's info. i.e. Department, display name, company, title, etc.

    i've been working in the code for a couple of days and i'm able to make the connection to AD and to verify that the user exists, but i really don't know how to display the user's information.

    Can you please help me? i need to finish this code today.

    Any help is very much appreciated.

    Thanks,

    Moises
    Sunday, May 4, 2008 5:14 PM

Answers

  • mabby216,

     

    Please try the following code snippet converted from here and change the necessary information according to your project:

     

    Code Snippet

            public void GetADUserInfo()

            {

                try {

                    //This is a generic LDAP call, it would do a DNS lookup to find a DC in your AD site, scales better

                    DirectoryEntry enTry = new DirectoryEntry("LDAP://OU=MyUsers,DC=Steve,DC=Schofield,DC=com");

                   

                    DirectorySearcher mySearcher = new DirectorySearcher(enTry);

                    mySearcher.Filter = "(&(objectClass=user)(anr=smith))";

                    SearchResult resEnt;

                    try {

                        foreach ( resEnt in mySearcher.FindAll()) {

                            Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item("cn").Value);

                            Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item("distinguishedName").Value);

                            Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item("name").Value);

                            Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item("givenName").Value);

                            Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item("displayName").Value);

                        }

                    }

                    catch (Exception f) {

                        Console.WriteLine(f.Message);

                    }

                }

                catch (Exception f) {

                    Console.WriteLine(f.Message);

                }

            }

     

     

    Actually there are many similar examples such as the article How to get domain user information from Active Directory in C# and the thread Getting Info From LDAP

     

    Hope that helps.

    Thursday, May 8, 2008 8:17 AM

All replies

  • Sunday, May 4, 2008 5:23 PM
  • Thanks for your info. But i'm still looking for a more specific way to display the user's information.

     

     

    Sunday, May 4, 2008 6:16 PM
  • mabby216,

     

    Please try the following code snippet converted from here and change the necessary information according to your project:

     

    Code Snippet

            public void GetADUserInfo()

            {

                try {

                    //This is a generic LDAP call, it would do a DNS lookup to find a DC in your AD site, scales better

                    DirectoryEntry enTry = new DirectoryEntry("LDAP://OU=MyUsers,DC=Steve,DC=Schofield,DC=com");

                   

                    DirectorySearcher mySearcher = new DirectorySearcher(enTry);

                    mySearcher.Filter = "(&(objectClass=user)(anr=smith))";

                    SearchResult resEnt;

                    try {

                        foreach ( resEnt in mySearcher.FindAll()) {

                            Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item("cn").Value);

                            Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item("distinguishedName").Value);

                            Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item("name").Value);

                            Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item("givenName").Value);

                            Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item("displayName").Value);

                        }

                    }

                    catch (Exception f) {

                        Console.WriteLine(f.Message);

                    }

                }

                catch (Exception f) {

                    Console.WriteLine(f.Message);

                }

            }

     

     

    Actually there are many similar examples such as the article How to get domain user information from Active Directory in C# and the thread Getting Info From LDAP

     

    Hope that helps.

    Thursday, May 8, 2008 8:17 AM
  • Hello,

    As most of the examples contain more than is needed and even some site dependant values here is my contribution for bit simpler data querying from your LDAP server.

    using System;
    using System.Text;
    using System.DirectoryServices; // You need to add this as reference in the project
    
    namespace GetADInfo
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Get the currently logged in user
                string name = Environment.UserName;
                // Get the currently connected LDAP context 
                DirectoryEntry entry1 = new DirectoryEntry("LDAP://RootDSE");
                string domainContext = entry1.Properties["defaultNamingContext"].Value as string;
                // Use the default naming context as the connected context may not work for searches
                DirectoryEntry entry = new DirectoryEntry("LDAP://" + domainContext);
                DirectorySearcher adSearch = new DirectorySearcher(entry);
    
                adSearch.Filter = "(&(objectClass=user)(anr="+name+"))";
    
                // Go through all entries from the active directory.
                foreach (SearchResult singleADUser in adSearch.FindAll())
                {
                    Console.WriteLine("The properties of the " + singleADUser.GetDirectoryEntry().Name +" are :");
                    // Go through all the values found in the search
                    foreach (string singleAttribute in ((ResultPropertyCollection)singleADUser.Properties).PropertyNames)
                    {
                        Console.WriteLine(singleAttribute + " = ");
                        foreach (Object singleValue in ((ResultPropertyCollection)singleADUser.Properties)[singleAttribute])
                        {
                            Console.WriteLine("\t" + singleValue);
                        }
                    }
                }
            }      
        }
    }
    

    If you wish to search for any other user than the one currently logged in, add method to input other name.

    Best Regards,

    Petteri Kujala

    • Proposed as answer by Petteri Kujala Friday, August 9, 2013 11:20 AM
    Friday, August 9, 2013 10:56 AM