从ASP.NET查询Active Directory并将结果绑定到列表视图

时间:2010-10-25 15:29:21

标签: c# asp.net vb.net active-directory activedirectorymembership

我设法用AD进行ASP.NET身份验证工作。现在,我想在AD中查询OU并显示结果 ASP.NET页面中的ListView或GridView。

  

这是域控制器:dc.itlab.edu

     

OU:UsersStudents

在组织单位(OU)UsersStudents中,有以下列:

名字,姓氏,Pre-Windows 2000登录名,名称,类型

我想在OU UsersStudents中查询列的名字,姓氏,Pre-Windows 2000登录名并绑定 结果到ListView或GridView。

感谢您在C#或VB.NET中提出的建议。

3 个答案:

答案 0 :(得分:4)

如果您使用的是.NET 3.5,或者可以升级到它,那么随着System.DirectoryServices.AccountManagement命名空间的引入,LDAP的内容已经大大改进

它包含诸如UserPrincipal之类的类,它提供了大多数常用的LDAP属性作为属性。使用PrincipalSearcher和QBE(按示例查询),您可以非常轻松地找到您感兴趣的用户(或其他对象)并将它们绑定到ASP.NET网格视图。

要了解有关新.NET 3.5的更多信息,请阅读MSDN杂志上的这篇优秀文章:

Managing Directory Security Principals in the .NET Framework 3.5 - January 2008 issue

更新:使用.NET 3.5界面,您可以编写如下代码:

// 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();

现在您应该可以将results直接绑定到DataGridView或其他内容,并为您要查找的列选择这些属性:

  • 名字= UserPrincipal.GivenName
  • 姓氏= UserPrincipal.Surname
  • Pre-Windows 2000 Logon Name = UserPrincipal.SamAccountName
  • 名称=姓名
  • Type = ??你的意思是什么?

答案 1 :(得分:0)

这里有populating an ASP.Net GridView from AD的C#示例。

答案 2 :(得分:0)

未经测试**这将指向正确的方向..应该非常接近您的需要。

    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()