Active Directory Searching with ASP.Net
Whenever I’ve Googled for and Visual Basic or C# code which will allow me to search through Active Directory I’ve had no luck. So, I wrote my own.
There are 2 parts to this project:
- A User Class which defines all of the properties related to AD.
- An Active Directory class which searches AD and creates an collection list made up of user objects which can then be bound to a Gridview or used as an object elsewhere.
There a number of functions in my Active Directory class such as find users by Groups, list all users in an AD group, select a single user from the list, etc. It could quite easily be expanded to meet your needs.
Below is a screen shot where I have implemented a Web Page which just allows for basic searching. You choose the AD property you want to search against and the results are displayed in a Gridview.
Download: Active Directory Class - Downloaded 295 Times
UPDATE: I missed out the ‘directReports’ property in Active Directory. To make use of this you need to add a new collection to the User class and populate it with a user objects.
e.g.
In User Class:
private _directReports as collection
public property directReports as collection
get
return _directReports
end get
set (byVal value as collection)
_directReports = value
end set
end property
In AD Class: (In the same way as groups work)










Thenx Bro Thanke u sooooooooo much
is there any way to get the user password…
I read similar article also named e Directory Searching with ASP.Net, and it was completely different. Personally, I agree with you more, because this article makes a little bit more sense for me
@Mohsin
Sorry for the late reply. I’ve been on holiday.
I presume that if you wanted to get user password then you just need to find out what the LDAP property is.
Then you would need to create a new Public Property for User.vb and add the following to AD.vb.
search.PropertiesToLoad.Add("PASSWORD_PROPERTY")I strongly suspect that you will find that passwords are encrypted though.
Pretty nice site, wants to see much more on it! :)t
Excellent Nick.
I have downloaded the above code. How do I create a connection to an Active Directory server?
I know we need to create DirectoryEntry(”LDAP://…”). Any ideas on how to do this and where to have this in the code?
Just want to test this on a live active directory.
@Erz
Thanks for the comments. On my network the code just connects automatically to Active Directory so just try it and see. The only reason you would need to introduce a connection string would be if you want to use a Proxy Account to access AD.