最佳解答者
How To 抓取所有的AD帳號

問題
-
解答
-
簡單的網域,只要用 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
-
兩個網址給您參考:
第一個我有試過,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=tw1 Imports System.DirectoryServices 2 3 Partial Class List_AD_User_1 4 Inherits System.Web.UI.Page 5 6 Protected Sub Page_Load(ByVal sender As Object, ByVal 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 String) As 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 29 End 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
-
小朱 表示:這裡有一個不錯的範例,稍微改寫後就行了。
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
所有回覆
-
簡單的網域,只要用 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
-
一樣使用 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
-
兩個網址給您參考:
第一個我有試過,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=tw1 Imports System.DirectoryServices 2 3 Partial Class List_AD_User_1 4 Inherits System.Web.UI.Page 5 6 Protected Sub Page_Load(ByVal sender As Object, ByVal 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 String) As 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 29 End 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
-
MIS2000 Lab_ 表示:
第一個我有試過,LDAP可以抓到資料:1 Imports System.DirectoryServices 2 3 Partial Class List_AD_User_1 4 Inherits System.Web.UI.Page 5 6 Protected Sub Page_Load(ByVal sender As Object, ByVal 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 String) As 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 29 End 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上面?
謝謝大家。 -
小朱 表示:這裡有一個不錯的範例,稍微改寫後就行了。
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