Searching Active Directory with .NET (Visual Studio 2005)

Sign in to queue

Description

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

https://channel9.msdn.com/Showpost.aspx?postid=130700
- Adding groups and users to groups with .NET

https://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:

https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdirectoryservicesdirectorysearcherclasssearchscopetopic.asp


~ Robert Shelton

Embed

Download

Download this episode

The Discussion

  • User profile image
    dlcarp
    Robert,

    I'm a VIsual Basic developer. Do you have this project available in Visual Basic code?

    Thanks,
    David Carpenter
  • User profile image
    Robert Shelton
    David, as funny as it sounds, I am a Visual Basic developer as well, but I've started doing demonstration code in C# to learn that language as well.  I don't currently have this in VB.NET, but if there is a lot of interest around a VB port, I will happily port the code and place it on the http://www.federaldeveloper.com site.

    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
  • User profile image
    smithm

    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.

  • User profile image
    hillbillybo​b77
    First of all...great work. I had been searching for a couple of days for exactly what your screencast showed. I am having some problems though.

    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!!
  • User profile image
    Natashenka

    Is there a way to view video in full screen?

  • User profile image
    smithm
    Try

    adObject.Properties["SAMAccountName"][0]
  • User profile image
    maro22
    it's great code,
    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

Add Your 2 Cents