本文將介紹如何以 DirectoryServices 讀取 Active Directory 中的資料。
在 .Net 用程式中想要讀取存放在 Active Directory 的資料,必須透過 System.DirectoryServices 這個類別所提供的方法,本文將對此作簡介。
首先必須先加入 System.DirectoryServices 參考,並在程式碼中 using System.DirectoryServices。
以下列程式碼來連接 Active Directory:
1: DirectoryEntry root = new DirectoryEntry("LDAP://網域名稱", "使用者名稱", "密碼");
2: DirectorySearcher search = new DirectorySearcher(root);
3: SearchResultCollection results = search.FindAll();
其中 DirectoryEntry 類別用來指定所要連接的網域名稱,以上面程式碼的第一列為例,其中網域名稱的部分,假設您的網域為 ms.com,則可輸入 LDAP://ms.com或 LDAP://ms。接著利用 DirectorySearcher 類別來對 Active Directory 進行查詢,最後將查詢結果存放至 SearchResultCollection 集合。
再來就可以開始讀取 SearchResult 中的屬性,下列程式碼示範讀取登入使用者名稱(samaccountname)、顯示名稱(displayname)、部門(department)以及物件類型(objectcategory):
1: if (results != null)
2: {
3: foreach (SearchResult result in results)
4: {
5: ResultPropertyCollection rpc = result.Properties;
6: string[] Properties = { "samaccountname", "displayname", "department", "objectcategory" };
7:
8: if (result.Properties[Properties[0]].Count > 0)
9: Console.WriteLine(string.Format("{0}={1}", Properties[0], result.Properties[Properties[0]][0]));
10:
11: if (result.Properties[Properties[1]].Count > 0)
12: Console.WriteLine(string.Format("{0}={1}", Properties[1], result.Properties[Properties[1]][0]));
13:
14: if (result.Properties[Properties[2]].Count > 0)
15: Console.WriteLine(string.Format("{0}={1}", Properties[2], result.Properties[Properties[2]][0]));
16:
17: if (result.Properties[Properties[3]].Count > 0)
18: Console.WriteLine(string.Format("{0}={1}", Properties[3], result.Properties[Properties[3]][0]));
19: }
20: }
屬性為應到 Active Directory 使用者與電腦中的物件如下圖所示:
完整程式碼如下:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.DirectoryServices;
6:
7: namespace DirectoryServicesDemo
8: {
9: class Program
10: {
11: static void Main(string[] args)
12: {
13: DirectoryEntry root = new DirectoryEntry("LDAP://網域名稱", "使用者名稱", "密碼");
14: DirectorySearcher search = new DirectorySearcher(root);
15: SearchResultCollection results = search.FindAll();
16:
17: if (results != null)
18: {
19: foreach (SearchResult result in results)
20: {
21: Console.WriteLine(result.Path);
22: ResultPropertyCollection rpc = result.Properties;
23: string[] Properties = { "samaccountname", "displayname", "department", "objectcategory" };
24:
25: if (result.Properties[Properties[0]].Count > 0)
26: Console.WriteLine(string.Format("{0}={1}", Properties[0], result.Properties[Properties[0]][0]));
27:
28: if (result.Properties[Properties[1]].Count > 0)
29: Console.WriteLine(string.Format("{0}={1}", Properties[1], result.Properties[Properties[1]][0]));
30:
31: if (result.Properties[Properties[2]].Count > 0)
32: Console.WriteLine(string.Format("{0}={1}", Properties[2], result.Properties[Properties[2]][0]));
33:
34: if (result.Properties[Properties[3]].Count > 0)
35: Console.WriteLine(string.Format("{0}={1}", Properties[3], result.Properties[Properties[3]][0]));
36: }
37:
38: //#region 列出所有屬性
39: foreach (SearchResult result in results)
40: {
41: ResultPropertyCollection rpc = result.Properties;
42: foreach (string key in rpc.PropertyNames)
43: {
44: foreach (object value in rpc[key])
45: {
46: Console.WriteLine(string.Format("{0}={1}", key, value));
47: }
48: }
49:
50: Console.WriteLine("----------------------------------------------------------");
51: }
52: // #endregion
53: }
54: Console.ReadKey();
55: }
56: }
57: }
【參考資料】