views:

54

answers:

3

I managed to do ASP.NET authentication work wih AD. Now, I want to query an OU in AD and display the result either ListView or GridView in ASP.NET page.

Here's the Domain Controller: dc.itlab.edu

The OU: UsersStudents

In the organizational unit (OU) UsersStudents there are following columns:

First Name, Last Name, Pre-Windows 2000 Logon Name, Name , Type

I want to query column First Name, Last Name, Pre-Windows 2000 Logon Name in OU UsersStudents and bind the result to ListView or GridView.

Thank you for suggestion either in C# or VB.NET.

A: 

There is a C# sample here for populating an ASP.Net GridView from AD.

Steve Townsend
A: 

Not tested** This will point you in the right direction.. Should be very close to what you need.

    Dim MySearchRoot As DirectoryEntry = New DirectoryEntry("LDAP://domain/DC=..", "usr", "pwd")
    Dim MyDirectorySearcher As New DirectorySearcher(MySearchRoot)

    MyDirectorySearcher.Filter = ("(&(objectCategory=organizationalunit)(name=UsersStudents))")

    MyDirectorySearcher.SearchScope = SearchScope.Subtree
    MyDirectorySearcher.PropertiesToLoad.Add("First Name")
    MyDirectorySearcher.PropertiesToLoad.Add("Last Name")
    MyDirectorySearcher.PropertiesToLoad.Add("Pre-Windows 2000 Logon Name")
    MyDirectorySearcher.PropertiesToLoad.Add("Name")
    MyDirectorySearcher.PropertiesToLoad.Add("Type")
    MyDirectorySearcher.Sort.Direction = System.DirectoryServices.SortDirection.Ascending
    MyDirectorySearcher.Sort.PropertyName = "Name"

    Dim MySearchResult As SearchResultCollection = MyDirectorySearcher.FindAll()

    Dim myTable As New DataTable("Results")
    Dim colName As String

    For Each colName In MyDirectorySearcher.PropertiesToLoad
        myTable.Columns.Add(colName, GetType(System.String))
    Next

    Dim result As SearchResult

    For Each result In MySearchResult
        Dim dr As DataRow = myTable.NewRow()
        For Each colName In MyDirectorySearcher.PropertiesToLoad
            If result.Properties.Contains(colName) Then
                    dr(colName) = CStr(result.Properties(colName)(0))
                End If
            Else
                dr(colName) = ""
            End If
        Next
        myTable.Rows.Add(dr)
    Next

    gridview.datasource = myTable
    gridview.databind()
mjw06d
+1  A: 

If you are on .NET 3.5, or could upgrade to it - the LDAP stuff has been vastly improved with the introduction of the System.DirectoryServices.AccountManagement namespace.

It contains among other things classes like UserPrincipal, which offers most of the commonly used LDAP attributes as properties. Using the PrincipalSearcher and QBE (Query-by-example), you could very easily find those users (or other objects) you're interested in and binding them to the ASP.NET grid view.

To learn more about the new .NET 3.5 stuff, read this excellent article at MSDN Magazine:

Managing Directory Security Principals in the .NET Framework 3.5

Update: Using the .NET 3.5 interface, you can write code something like this:

// define the content - domain name (second param) must be NetBIOS-style,
// third parameter is the container where to create the context for
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "ITLAB", "OU=UsersStudents,DC=dc,DC=itlab,DC=edu");

// define your "prototype" for the searcher - here: you want to search for 
// users which have the .Enabled property set to true; you could define additional
// requirements here
UserPrincipal qbePrototype = new UserPrincipal(ctx);
qbePrototype.Enabled = true;

// create PrincipalSearcher based on that QBE prototype
PrincipalSearcher ps = new PrincipalSearcher(qbePrototype);

// find all matching Principals - in your case, those will be of type UserPrincipal
PrincipalSearchResult<Principal> results = ps.FindAll();

Now you should be able to bind the results directly to a DataGridView or something, and pick out those properties for your columns that you're looking for:

  • First Name = UserPrincipal.GivenName
  • Last Name = UserPrincipal.Surname
  • Pre-Windows 2000 Logon Name = UserPrincipal.SamAccountName
  • Name = Name
  • Type = ?? What you do mean here??
marc_s
Yes, I'm using .NET 3.5
Narazana
There's column "Type" in the OU. Type -> User
Narazana
If you're only searching for Users, then this Type will always be User anyway.....
marc_s
I keep getting this error message System.DirectoryServices.DirectoryServicesCOMException: There is no such object on the server . I think somewhere along this line (ContextType.Domain, "itlab", "OU=UsersStudents,DC=DC,DC=itlab,DC=edu") has an error because when I use only PrincipalContext(ContextType.Domain) I got result back. But how to query particular OU then?
Narazana
@Narazana: I don't **know** what your domain is called Netbios-style - I just **guessed** it might be "itlab" - if it's not, use that instead! Same applies to the LDAP path for the container - that's just a guess based on your post - maybe it's not 100% accurate - you need to verify that and change if necessary. That third parameter defines **where** in the hierarchy you're creating your context - in this case, in the "UsersStudents" OU - change this if needed.
marc_s
Finally, I got it after 3hr straight and with help of marc_s. **Dim ctx As New PrincipalContext(ContextType.Domain, "DC", "OU=UsersStudents,DC=itlab,DC=edu")** the second param "dc" (case insensitive) is NetBiosName of domain controller. Anywhere, is there any method to**query only a certain number of users** Not all of them?
Narazana
@Narazana: I don't think you can limit the number of entries you get back - but you **could** e.g. search by lastname, e.g. define `qbePrototype.Surname = "A*"` to search for everyone with a lastname beginning with an "A", and then go through the list that way, letter by letter.
marc_s