Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How to retrieve all active users from Acitve Directory thru C#

Posted on 2011-02-22
Medium Priority
Last Modified: 2012-05-11
Hi Everyone,

Already i have code for retrieve user info from AD based on the string but am trying to include new features to find the all active users in the AD.

What is the best approach i can go for it,i don't find any filter property to find active users in AD.

Please give me your suggestion.
Question by:Sha1395
  • 5
  • 4

Expert Comment

ID: 34957470
This seems to answer your question, though I can't say I've tried it.


In particular, the LDAP search query for all active users provided at that link is given by:


For disabled users, change 512 to 514.

You may wish to read that article more thoroughly than I did though to understand this better though.

Author Comment

ID: 34957728

I was trying to retrieve the value of "userAccountControl".The reason for that is

userAccountControl= "512" for active and "514" for inactive user.

am going to use this preoperty in my filter to retrieve only Active users from AD.

when i use "userAccountControl" in my code,it doesn't return any value.please guide me,am i using the right property and name ?

Thanks in Advance


Expert Comment

ID: 34957771
Can you post your code and I'll see if I can see anything obviously wrong.

I must confess that the information I gave you above was not something I'm familiar with, just a link that seemed to answer your question, which might have given you some helpful information, or at least a starting point from which to research more. I'm not 100% that it's correct for you.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 34957791
sure,below is my code to retrieve user info.

am trying to do two things here

1) retrieve the UserAccountControl value (ie: 512 or 514)
2) Add another function in this code,when the button clicks in the window console,am going to call this function with reference of this dll

to show the active user (user name) as a return.

//Credit goes to Dhaest

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace ActiveDir
    public class Employee
    // instance variables 
    public String name;
    public String surname;
    public string title;
    public string company;
    public string email,useraccountcontrol;
    public string mobile, telephonenumber,pager;
    public string city, state, country, postalcode;
        //   ...
    // Constructors

    public String FullName()
        return String.Format("{0}. {1} {2}. {3}. {4}. {5}. {6}. {7}. {8}. {9}. {10}. {11}. {12} ", name, title, surname, company,email,pager, mobile, telephonenumber,city,state, postalcode,country,useraccountcontrol).Trim();
        //return(String.Format("{0,-20} : {1}",title) );
    public static implicit operator String(Employee value)
        return value.ToString();

    public Employee()

    public class ActiveDirSearch
        public ActiveDirSearch()
        { }

        public Employee SearchUser(string username)
            Employee employee = new Employee();

            if (username.Contains("."))
                string[] splitString = username.Split(new char[] { '.' });
                username = splitString[splitString.Length - 1];

                //DirectoryEntry entry = new DirectoryEntry("GC://Test", "Test\\Test", "Ld@Test", AuthenticationTypes.Secure);

                // DirectorySearcher search = new DirectorySearcher(myLdapConnection);
                System.DirectoryServices.DirectorySearcher search = new System.DirectoryServices.DirectorySearcher(entry);
                //  search.Filter = "(cn=" + username + ")";

                if (username.IndexOf(" ") > 0)
                    search.Filter = "(cn=" + username + ")";
                    search.Filter = "(sn=" + username + ")";

                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])
                                 if (ldapField == "name") 
                                     employee.name = myCollection.ToString();
                                 if (ldapField == "surname")
                                     employee.surname = myCollection.ToString();
                                 if (ldapField == "company")
                                     employee.company = myCollection.ToString();
                                 if (ldapField == "mobile")
                                     employee.mobile = myCollection.ToString();
                                 if (ldapField == "postalcode")
                                     employee.postalcode = myCollection.ToString();
                                 if (ldapField == "telephonenumber")
                                     employee.telephonenumber = myCollection.ToString();
                                 if (ldapField == "l")
                                     employee.city = myCollection.ToString();
                                 if (ldapField == "st")
                                     employee.state = myCollection.ToString();
                                 if (ldapField == "co")
                                     employee.country = myCollection.ToString();
                                 if (ldapField == "mail")
                                     employee.email = myCollection.ToString();
                                 if (ldapField == "otherpager")
                                     employee.pager = myCollection.ToString();
                                 if (ldapField == "userAccountControl")
                                     employee.useraccountcontrol = myCollection.ToString();



                   return null;

            catch (Exception e)
                throw e;

            return employee ;

Open in new window


Expert Comment

ID: 34957843
Is your OS in the applies to section at this link?


That maybe why you're not getting anything. The useraccountcontrol property may only be valid in certain versions of windows... perhaps, though I'm guessing a little here.

Author Comment

ID: 34966523
Thanks crysallus ,i was going trhu all the MSDN.

I tried again with "Useraccountcontrol" it worked like a charm,am sure i might did some mistake in the to call Useraccount control.

Anyway thanks a lot for your help to give me more understand about the topic.

Author Comment

ID: 34966527
if (ldapField == "useraccountcontrol")
                                     employee.useraccountcontrol = myCollection.ToString();

Accepted Solution

crysallus earned 1500 total points
ID: 34966574
My comments provided meaningful help, as admitted by the author. At least a B I would have thought.

Author Closing Comment

ID: 34966798
Help me to understand the problem but got my own way to resolved the issues.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

578 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