Searching Active Directory with .NET (Visual Studio 2005)
- Posted: Nov 02, 2005 at 6:56 PM
- 96,748 Views
- 7 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
Federal Developer Evangelist, Robert Shelton, takes you through a 12 minute walkthrough/demonstration of how to search Active Directory for users, groups, and other AD Objects.
This demonstration is using the DirectoryServices namespace of the .NET framework. The demonstration is using Visual Studio 2005, but the code will also work as written for Visual Studio 2003.
You can find the code at my blog:
http://sheltonblog.com
My other AD Screencasts:
- Adding user to AD with .NET
http://channel9.msdn.com/Showpost.aspx?postid=130700
- Adding groups and users to groups with .NET
http://channel9.msdn.com/Showpost.aspx?postid=132400
- AD Searchfilter (Querying) Syntax:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/search_filter_syntax.asp
- List of SearchScope options:
~ Robert Shelton
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
I'm a VIsual Basic developer. Do you have this project available in Visual Basic code?
Thanks,
David Carpenter
Currently I am working on some web cast for Windows Communication Foundation, otherwise, I would just do it now. After I finish those, I will look at porting all of my screencast to both VB and C#. This way, no one gets left out and it's easier for me, since it is more natural for me to read/write in VB.
Robert
I couldn't find the VB-Code in the net, so I just ported it myself:
' If you want to search in a specific path, here's the right spot.
' Just insert the path into "As New DirectoryEntry("LDAP://OU=Accounting,DC=World,DC=com")"
Dim Entry As New DirectoryEntry
Dim Searcher As New DirectorySearcher(Entry)
Dim AdObj As SearchResult
Searcher.SearchScope = SearchScope.Subtree
Searcher.Filter() = "(ObjectClass=user)"
For Each AdObj In Searcher.FindAll
Label1.Text = Label1.Text & "CN=" & AdObj.Properties("CN").Item(0) & " | Path=" & AdObj.Path & "<br>"
Next
I coded it with ASP.net for a webapplication.
But the App does exactely the same as the first example.
I hope you can use it.
Instead of the common name, I would like to display the users logon name. I have been messing around, I got a look at the target's AD schema, and have found some promising attributes to plug in. However, If I try to use and attribute that is not "mandatory" I get a "ArgumentOutOfRangeException"
Here is my code (almost exact to yours)
DirectoryEntry adDirecEntry = new DirectoryEntry();
DirectorySearcher adDirecSearch = new DirectorySearcher(adDirecEntry);
//set the search scope
adDirecSearch.SearchScope = SearchScope.Subtree;
//Set the filter. For this example we will be looking at all users
adDirecSearch.Filter = "(ObjectClass=user)";
//Execute the search and iterate through the result
//Write results to lboAD for display
foreach (SearchResult adObject in adDirecSearch.FindAll())
{
lboAD.Items.Add(adObject.Properties["uid"][0]);
lboAD.Items.Add(adObject.Path);
}
It is at "lboAD.Items.Add(adObject.Properties["uid"][0]);" that I get the exception. At first I thought it was maybe something else, I dunno what....but it will only display results when I use a mandatory attribute, no matter the ObjectClass.
Is it something to do with my target's AD schema? Is it something I am doing wrong?
Thanks, and again, great work!!
Is there a way to view video in full screen?
adObject.Properties["SAMAccountName"][0]
but i'm using visual studio 2003 & when i run the code it gives me "The specified domain either does not exist or could not be contacted "
so please advice
thanks
Remove this comment
Remove this thread
close