Solved

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

Posted on 2011-02-22
9
497 Views
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.
0
Comment
Question by:Sha1395
  • 5
  • 4
9 Comments
 
LVL 8

Expert Comment

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

http://anandthearchitect.wordpress.com/2008/07/02/list-all-active-users-in-a-domain/

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

(&(&(objectCategory=user)(userAccountControl=512)))

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.
0
 

Author Comment

by:Sha1395
ID: 34957728
Hi,

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


0
 
LVL 8

Expert Comment

by:crysallus
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.
0
 

Author Comment

by:Sha1395
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];
            }


            try
            {
                //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 + ")";
                else
                    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=="title")
                                     employee.title=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();

                            }
                      }

                 }

                         else
                        {
                   return null;
                      }
            }

            catch (Exception e)
            {
                throw e;
            }

            return employee ;
        }
    }
}

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 8

Expert Comment

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

http://support.microsoft.com/kb/305144#appliesto

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.
0
 

Author Comment

by:Sha1395
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.
0
 

Author Comment

by:Sha1395
ID: 34966527
if (ldapField == "useraccountcontrol")
                                     employee.useraccountcontrol = myCollection.ToString();
0
 
LVL 8

Accepted Solution

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

Author Closing Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

12 Experts available now in Live!

Get 1:1 Help Now