none
How To 抓取所有的AD帳號 RRS feed

  • 問題

  • 關於AD帳號的認證,我找了一些文章,
    包含小朱、Will的BLOG,也找過google

    大部分都是透過AD來作認證

    我有找到 System.Security.Principal NameSapce,有一個NTAccount Class
    http://msdn.microsoft.com/en-us/library/system.security.principal.ntaccount.aspx
    但很可惜,裡面都沒有範例可以參考。

    我目前遇到的問題,是要把全部的AD帳號,複製到自己的DB裡面

    不知道有沒有相關的資料,可以提供我參考?
    謝謝各位


    2009年2月9日 下午 01:30

解答

  • 簡單的網域,只要用 DirectorySearcher 就可以解決了(搭配 objectCategory=user)。
    複雜的網域或是有階層性的網域,可以由 rootDSE,或者是樹系的最頂端來依序瀏覽(System.DirectoryServices.ActiveDirectory 命名空間有一些工具類別可以用),網域中有 OU 的話也可以用這樣的方法來做。
    MVP 2009 (ASP.NET), MCPD: ASP.NET Developer 3.5, MCPD: Windows Developer 3.5, MCITP: Database Developer 3.5, MCITP: Enterprise Administrator 不想被人認為是小白,就不要總是在做一堆會讓人認為是小白的事。
    • 已提議為解答 Lolota Lee 2009年2月13日 上午 10:41
    • 已標示為解答 Lolota Lee 2009年2月13日 上午 10:41
    2009年2月9日 下午 03:01
    版主
  •  http://www.codeproject.com/KB/IP/LDAP_Using_VBnet.aspx

    參考這範例就可以抓出AD裡的資料
    • 已標示為解答 A.W. _ 2009年2月16日 上午 07:52
    2009年2月10日 上午 07:23
  • 兩個網址給您參考:

    第一個我有試過,LDAP可以抓到資料:
    因為這個網頁被鎖住。我是透過Google的歷史資料看見的
    http://72.14.235.132/search?q=cache:sZh1qo-0OI8J:www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23695862.html+LDAP+Users+List&hl=zh-TW&ct=clnk&cd=6&gl=tw

    1Imports System.DirectoryServices 
    2 
    3Partial Class List_AD_User_1 
    4    Inherits System.Web.UI.Page 
    5 
    6    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load 
    7        CheckBoxList1.DataSource = GetUsers("LDAP的字串"
    8        CheckBoxList1.DataBind() 
    9    End Sub 
    10 
    11 
    12    Public Function GetUsers(ByVal Domain As StringAs String() 
    13        Dim userList As New List(Of String
    14 
    15        'Dim dEntry As DirectoryEntry = New DirectoryEntry("LDAP://" & Domain) 
    16        Dim dSearcher As DirectorySearcher = New DirectorySearcher(dEntry) 
    17 
    18        dSearcher.Filter = ("(objectClass=*)"
    19 
    20 
    21        Dim sResult As SearchResult  
    22        For Each sResult In dSearcher.FindAll()   
    23            userList.Add(sResult.GetDirectoryEntry().Name.ToString()) 
    24        Next 
    25 
    26        Return userList.ToArray() 
    27    End Function 
    28 
    29End Class 



    第二個我沒親身試過,僅供參考
    http://www.codeproject.com/KB/cs/groupandmembers.aspx

    我的ASP.NET教學網站 http://www.taconet.com.tw/mis2000_aspnet/
    • 已標示為解答 A.W. _ 2009年2月16日 上午 07:52
    2009年2月12日 上午 09:47
  • WinNT 提供者不支援 DirectorySearcher 的搜尋功能,你只能用 DirectoryEntry 來列舉。

    先用 WinNT://電腦名稱進入本機的根目錄,再使用 Children 屬性去瀏覽每個子物件,使用者的 ObjectClass 是 User。

    • 已標示為解答 A.W. _ 2009年2月20日 上午 07:15
    2009年2月16日 上午 08:20
    版主
  • 小朱 表示:

    WinNT 提供者不支援 DirectorySearcher 的搜尋功能,你只能用 DirectoryEntry 來列舉。

    先用 WinNT://電腦名稱進入本機的根目錄,再使用 Children 屬性去瀏覽每個子物件,使用者的 ObjectClass 是 User。

    這裡有一個不錯的範例,稍微改寫後就行了。
    資料來源:http://msdn.microsoft.com/zh-tw/library/87tye19w(VS.80).aspx


            Dim objDE As DirectoryEntry
            '--下面這一行,是系統自動尋找本機名稱
            Dim strPath As String = "WinNT://" + Environment.MachineName
            '==================================================

            objDE = New DirectoryEntry(strPath)

            Dim output_string As String = Nothing

            Dim objChildDE As DirectoryEntry
            For Each objChildDE In objDE.Children
                If objChildDE.SchemaClassName = "User" Then   '--只搜尋使用者名稱
                    output_string = output_string + objChildDE.Name & "<br>"
                    '--列出所有物件(使用者、群組等等)的名稱
                End If
            Next objChildDE

            Label1.Text = output_string

    我的ASP.NET教學網站 http://www.taconet.com.tw/mis2000_aspnet/
    • 已標示為解答 A.W. _ 2009年2月20日 上午 07:15
    2009年2月18日 上午 01:53

所有回覆

  • 簡單的網域,只要用 DirectorySearcher 就可以解決了(搭配 objectCategory=user)。
    複雜的網域或是有階層性的網域,可以由 rootDSE,或者是樹系的最頂端來依序瀏覽(System.DirectoryServices.ActiveDirectory 命名空間有一些工具類別可以用),網域中有 OU 的話也可以用這樣的方法來做。
    MVP 2009 (ASP.NET), MCPD: ASP.NET Developer 3.5, MCPD: Windows Developer 3.5, MCITP: Database Developer 3.5, MCITP: Enterprise Administrator 不想被人認為是小白,就不要總是在做一堆會讓人認為是小白的事。
    • 已提議為解答 Lolota Lee 2009年2月13日 上午 10:41
    • 已標示為解答 Lolota Lee 2009年2月13日 上午 10:41
    2009年2月9日 下午 03:01
    版主
  •  http://www.codeproject.com/KB/IP/LDAP_Using_VBnet.aspx

    參考這範例就可以抓出AD裡的資料
    • 已標示為解答 A.W. _ 2009年2月16日 上午 07:52
    2009年2月10日 上午 07:23
  • 謝謝上面兩位朋友的大力協助。

    請教各位:

    如果是一台 Win 2003 Server,「沒有」升級成AD
    用程式抓取它所有的 Users(Account) 或是Roles 列表

    該怎麼作呢?
    或是要使用哪些 NameSpace ?

    謝謝大家。找了幾天資料,但還是沒有頭緒。
    2009年2月11日 上午 08:07
  • 一樣使用 DirectorySearcher,但是搜尋的路徑要改用 WinNT://[電腦名稱],LDAP:// 是給 AD 用的。
    MVP 2009 (ASP.NET), MCPD: ASP.NET Developer 3.5, MCPD: Windows Developer 3.5, MCITP: Database Developer 3.5, MCITP: Enterprise Administrator 不想被人認為是小白,就不要總是在做一堆會讓人認為是小白的事。
    • 已提議為解答 Lolota Lee 2009年2月13日 上午 10:40
    • 已標示為解答 Lolota Lee 2009年2月13日 上午 10:41
    • 已取消標示為解答 A.W. _ 2009年2月20日 上午 07:15
    2009年2月11日 上午 08:27
    版主
  • 兩個網址給您參考:

    第一個我有試過,LDAP可以抓到資料:
    因為這個網頁被鎖住。我是透過Google的歷史資料看見的
    http://72.14.235.132/search?q=cache:sZh1qo-0OI8J:www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23695862.html+LDAP+Users+List&hl=zh-TW&ct=clnk&cd=6&gl=tw

    1Imports System.DirectoryServices 
    2 
    3Partial Class List_AD_User_1 
    4    Inherits System.Web.UI.Page 
    5 
    6    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load 
    7        CheckBoxList1.DataSource = GetUsers("LDAP的字串"
    8        CheckBoxList1.DataBind() 
    9    End Sub 
    10 
    11 
    12    Public Function GetUsers(ByVal Domain As StringAs String() 
    13        Dim userList As New List(Of String
    14 
    15        'Dim dEntry As DirectoryEntry = New DirectoryEntry("LDAP://" & Domain) 
    16        Dim dSearcher As DirectorySearcher = New DirectorySearcher(dEntry) 
    17 
    18        dSearcher.Filter = ("(objectClass=*)"
    19 
    20 
    21        Dim sResult As SearchResult  
    22        For Each sResult In dSearcher.FindAll()   
    23            userList.Add(sResult.GetDirectoryEntry().Name.ToString()) 
    24        Next 
    25 
    26        Return userList.ToArray() 
    27    End Function 
    28 
    29End Class 



    第二個我沒親身試過,僅供參考
    http://www.codeproject.com/KB/cs/groupandmembers.aspx

    我的ASP.NET教學網站 http://www.taconet.com.tw/mis2000_aspnet/
    • 已標示為解答 A.W. _ 2009年2月16日 上午 07:52
    2009年2月12日 上午 09:47
  • MIS2000 Lab_ 表示:


    第一個我有試過,LDAP可以抓到資料:

    1Imports System.DirectoryServices 
    2 
    3Partial Class List_AD_User_1 
    4    Inherits System.Web.UI.Page 
    5 
    6    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load 
    7        CheckBoxList1.DataSource = GetUsers("LDAP的字串"
    8        CheckBoxList1.DataBind() 
    9    End Sub 
    10 
    11 
    12    Public Function GetUsers(ByVal Domain As StringAs String() 
    13        Dim userList As New List(Of String
    14 
    15        Dim dEntry As DirectoryEntry = New DirectoryEntry("LDAP://" & Domain) 
    16        Dim dSearcher As DirectorySearcher = New DirectorySearcher(dEntry) 
    17 
    18        dSearcher.Filter = ("(objectClass=*)"
    19 
    20 
    21        Dim sResult As SearchResult  
    22        For Each sResult In dSearcher.FindAll()   
    23            userList.Add(sResult.GetDirectoryEntry().Name.ToString()) 
    24        Next 
    25 
    26        Return userList.ToArray() 
    27    End Function 
    28 
    29End Class 



    我的ASP.NET教學網站 http://www.taconet.com.tw/mis2000_aspnet/


    非常感謝。
    您提供的程式是可以運作的。

    連上公司的Windows AD,可以正確抓取資料。

    但如果把上述的LDAP://   依照小朱的建議,修改成 WinNT://
    就會出錯了。

    不知道何故?


    提供者不支援搜尋,無法搜尋 WinNT://Acer_4105NB。 

    '/AD_Principal' 應用程式中發生伺服器錯誤。

    提供者不支援搜尋,無法搜尋 WinNT://PC001。

    描述: 在執行目前 Web 要求的過程中發生未處理的例外情形。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。

    例外詳細資訊: System.NotSupportedException: 提供者不支援搜尋,無法搜尋 WinNT://PC001。


    請教各位,是哪裡出錯呢?

    還是上面的寫法,不可以用在 Win xp與Win vista上面?

    謝謝大家。

    2009年2月16日 上午 07:55
  • WinNT 提供者不支援 DirectorySearcher 的搜尋功能,你只能用 DirectoryEntry 來列舉。

    先用 WinNT://電腦名稱進入本機的根目錄,再使用 Children 屬性去瀏覽每個子物件,使用者的 ObjectClass 是 User。

    • 已標示為解答 A.W. _ 2009年2月20日 上午 07:15
    2009年2月16日 上午 08:20
    版主
  • 小朱 表示:

    WinNT 提供者不支援 DirectorySearcher 的搜尋功能,你只能用 DirectoryEntry 來列舉。

    先用 WinNT://電腦名稱進入本機的根目錄,再使用 Children 屬性去瀏覽每個子物件,使用者的 ObjectClass 是 User。

    這裡有一個不錯的範例,稍微改寫後就行了。
    資料來源:http://msdn.microsoft.com/zh-tw/library/87tye19w(VS.80).aspx


            Dim objDE As DirectoryEntry
            '--下面這一行,是系統自動尋找本機名稱
            Dim strPath As String = "WinNT://" + Environment.MachineName
            '==================================================

            objDE = New DirectoryEntry(strPath)

            Dim output_string As String = Nothing

            Dim objChildDE As DirectoryEntry
            For Each objChildDE In objDE.Children
                If objChildDE.SchemaClassName = "User" Then   '--只搜尋使用者名稱
                    output_string = output_string + objChildDE.Name & "<br>"
                    '--列出所有物件(使用者、群組等等)的名稱
                End If
            Next objChildDE

            Label1.Text = output_string

    我的ASP.NET教學網站 http://www.taconet.com.tw/mis2000_aspnet/
    • 已標示為解答 A.W. _ 2009年2月20日 上午 07:15
    2009年2月18日 上午 01:53