How to read Active directory userlist using c#

hi all,

i have an active directory structured as follows

*************************************

    Active Directory Users and Computers[company1.company.com]

    saved queries

    - company.com

                  aa

                  built in      

                  Computers

                  Domain Controllers

                  Foreign Security principles

                   Users

*****************************************

 

my aim is to get the Users list and diaplay it in a list

i used the sollowing code

-----------------------------------

DirectoryEntry dentry = new DirectoryEntry("LDAP://rootDSE");

DirectorySearcher dsearch = new DirectorySearcher(dentry);

string name = "Users";

dsearch.Filter="(&(objectClass=user)(cn=" + name + "))";

foreach (SearchResult sResultSet in dsearch.FindAll())

{

MessageBox.Show(sResultSet.GetDirectoryEntry().Path.ToString());

}

i am getting exceptions that i can not read the directory

i am new to c#

any guide lines and if possible some code pls

regards

ranu

 

 

 



Answer this question

How to read Active directory userlist using c#

  • fieldhouse

    Hi

    did you try stepping through your code with the debugger

    If so, at which line do you get your exception

    could it be that you have no rights to read the active directory



  • talha zia

    DirectoryEntry dentry = new DirectoryEntry("LDAP://Domainname","username",password");

    DirectorySearcher dsearcher = new DirectorySearcher(dentry);

    dsearcher.Filter = ("(objectClass=user)");

    foreach (SearchResult sres in dsearcher.FindAll())

    {

    Response.Write(sres.GetDirectoryEntry().Name.ToString()+"<br>");

    }

    By using this code you get the list of users in the active directory

    You have to give the domain name,username and password.


  • Paul X

    hi frederikm

    i am getting exception at the foreach statement  "dsearch.findall()" method.

    and

    the exception i am getting is

    System.Runtime.InteropServices.COMException (0x8007203A): The server is not operational.

    what it exatly means

    and what should i do

     

    can u please tell me the way i am aproaching that is

    DirectoryEntry dentry = new DirectoryEntry(LDAP://rootDSE);

    is correct or not

    and please tell me the way to get "Users" conateiner list

    is this works

     

    ds.Filter = "(objectClass=Users)";


    thanks

    ranu


  • How to read Active directory userlist using c#