Friday, September 19, 2008

How to Query Active Directory for Users and Groups..

Last couple of months I was really busy working on a small ASP.Net project from scratch....
Alright... enough of that.Lets get to the Code....

First of all I want to show you guys how I divided the whole application into small components... here's is a screen shot of the app...


PS: Please click on the image to view the contents.

Each component is a stand alone component for a specific purpose....
So.. lets get into the Auth component...

We had a requirement where we wanted to Authenticate a user against the Active Directory...basically certain pages were not visible to certain groups...

Querying Active Directory in c# is very easy task.. but before that if you are not familiar with the structure of your active directory you can install a great utility from Microsoft called ldp.exe.

I tried it to see what is the structure of our Active Directory.

So basically to query an Active Directory you need a ldappath... like
LDAP://NXdfsdfqwM03.ABC150.org/DC=ABC150,DC=org , A username and password who has access to this Active Directory.

Now to get All the Users in a particular Group this is the code...


This function bascially calls the GetUserByGroup which in turn calls the GetUserDetails function where I am querying the active directory for a particular user.


PS: Please click on the image to view contents...


PS: Please click on the image to view contents...


PS:Please click on the image to view contents...

Important classes are DirectoryEntry, DirectorySearcher, DirectoryEntires.. etc.

I hope this gives a basic idea about how to query an Active Directory.

Happy Programming!!!
Yash