locked
asp.net & Active Directory RRS feed

  • Question

  • User1811435116 posted

    I have to populate a grid view with a complete list of all users signed in a Active Directory

    how can I do that?

    thanks..

    Tuesday, September 11, 2007 9:47 AM

Answers

  • User-704965582 posted
    Here is an example in VB.
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    	GridView1.DataSource = dtAdQuery()
    GridView1.DataBind()
    End Sub Public Function dtAdQuery() As Data.DataTable Dim de As New DirectoryEntry("LDAP://myserver.something.com", Username, Password, AuthenticationTypes.Secure) Dim deFilter As String = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)" ' Ignore disabled accts Using de Dim ds As New DirectorySearcher(de, deFilter) Using ds ds.PropertiesToLoad.Add("sn") ds.PropertiesToLoad.Add("givenName") ds.PageSize = 1 ds.SearchScope = SearchScope.Subtree ds.Sort.PropertyName = "sn" Dim Results As SearchResultCollection Results = ds.FindAll() Dim dt As New Data.DataTable Dim dr As Data.DataRow dt.Columns.Add("LastName", GetType(String)) dt.Columns.Add("FirstName", GetType(String)) For Each Result As SearchResult In Results dr = dt.NewRow If (Result.Properties.Contains("sn")) Then dr(0) = Result.Properties("sn")(0) End If If (Result.Properties.Contains("givenName")) Then dr(1) = Result.Properties("givenName")(0) End If dt.Rows.Add(dr) Next Return dt End Using End Using End Function
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 11, 2007 10:41 AM

All replies

  • User-704965582 posted
    Here is an example in VB.
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    	GridView1.DataSource = dtAdQuery()
    GridView1.DataBind()
    End Sub Public Function dtAdQuery() As Data.DataTable Dim de As New DirectoryEntry("LDAP://myserver.something.com", Username, Password, AuthenticationTypes.Secure) Dim deFilter As String = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)" ' Ignore disabled accts Using de Dim ds As New DirectorySearcher(de, deFilter) Using ds ds.PropertiesToLoad.Add("sn") ds.PropertiesToLoad.Add("givenName") ds.PageSize = 1 ds.SearchScope = SearchScope.Subtree ds.Sort.PropertyName = "sn" Dim Results As SearchResultCollection Results = ds.FindAll() Dim dt As New Data.DataTable Dim dr As Data.DataRow dt.Columns.Add("LastName", GetType(String)) dt.Columns.Add("FirstName", GetType(String)) For Each Result As SearchResult In Results dr = dt.NewRow If (Result.Properties.Contains("sn")) Then dr(0) = Result.Properties("sn")(0) End If If (Result.Properties.Contains("givenName")) Then dr(1) = Result.Properties("givenName")(0) End If dt.Rows.Add(dr) Next Return dt End Using End Using End Function
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 11, 2007 10:41 AM
  • User1811435116 posted

    thanks but the program give me an error on username and password..

    are they string or what else?

    Tuesday, September 11, 2007 11:37 AM
  • User1811435116 posted

    there is also another error like: server not operable

    Tuesday, September 11, 2007 11:41 AM
  • User-704965582 posted

    Depending on the permissions you could try removing the username and password.

    Replace:

    Dim de As New DirectoryEntry("LDAP://myserver.something.com", Username, Password, AuthenticationTypes.Secure)

    with

    Dim de As New DirectoryEntry(LDAP://myserver.something.com)

    Tuesday, September 11, 2007 12:35 PM
  • User172347472 posted

    I need to do the same thing, but the adspath with OU=Users

    I have tried to do it like this

    Dim de As New DirectoryEntry("LDAP://myserver.something.com/OU=Users", Username, Password, AuthenticationTypes.Secure)
    or 
    Dim de As New DirectoryEntry("LDAP://myserver.something.com/OU=Users, DC=corp", Username, Password, AuthenticationTypes.Secure)
    But when I do that I get error in  Results = ds.FindAll()
     Any ideas? I am completely lost at this point [:|] 

     

    P.S. I was able to do work around for this issue.

    I have added this

    If (Result.Path.Contains("OU=Users")) Then

    ... 

    End IF

    In area where the table is being formed.  

    Wednesday, November 7, 2007 2:31 AM