View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.DirectoryServices;
6 using System.Runtime.InteropServices;
7 using System.Security.Principal;
8 using System.Configuration;
9
10 public class ADHelper
11 {
12 ///
13
14 ///域名
15
16 ///
17
18 private static string DomainName = ConfigurationManager.AppSettings["DomainName"];
19
20 ///
21
22 /// LDAP绑定路径
23
24 ///
25
26 private static string ADPath = "LDAP://" + DomainName;
27
28 ///
29
30 ///登录帐号
31
32 ///
33
34 private static string ADUser = ConfigurationManager.AppSettings["ADUser"];
35 ///
36
37 ///登录密码
38
39 ///
40
41 private static string ADPassword = ConfigurationManager.AppSettings["ADPassword"];
42
43
44 /// <summary>
45 /// 用户属性定义
46 /// </summary>
47 public enum ADUserProperty
48 {
49 Name,//username
50 UserPrincipalName,//email
51 homePhone//tel
52 }
53
54 #region GetDirectoryObject
55
56 ///
57
58 ///获得DirectoryEntry对象实例,以管理员登陆AD
59
60 ///
61
62 ///
63
64 private static DirectoryEntry GetDirectoryObject()
65 {
66
67 DirectoryEntry entry = new DirectoryEntry(ADPath, ADUser, ADPassword, AuthenticationTypes.Secure);
68
69 return entry;
70
71 }
72
73
74
75 ///
76
77 ///根据指定用户名和密码获得相应DirectoryEntry实体
78
79 ///
80
81 private static DirectoryEntry GetDirectoryObject(string userName, string password)
82 {
83
84 DirectoryEntry entry = new DirectoryEntry(ADPath, userName, password, AuthenticationTypes.None);
85
86 return entry;
87
88 }
89
90
91
92 ///
93
94 /// i.e. /CN=Users,DC=creditsights, DC=cyberelves, DC=Com
95
96 ///
97
98 ///
99
100 ///
101
102 private static DirectoryEntry GetDirectoryObject(string domainReference)
103 {
104
105 DirectoryEntry entry = new DirectoryEntry(ADPath + domainReference, ADUser, ADPassword, AuthenticationTypes.Secure);
106
107 return entry;
108
109 }
110
111
112
113 ///
114
115 ///获得以UserName,Password创建的DirectoryEntry
116
117 ///
118
119 private static DirectoryEntry GetDirectoryObject(string domainReference, string userName, string password)
120 {
121
122 DirectoryEntry entry = new DirectoryEntry(ADPath + domainReference, userName, password, AuthenticationTypes.Secure);
123
124 return entry;
125
126 }
127
128
129
130 #endregion
131
132
133 public static IList<string> GetAllUserName()
134 {
135 DirectoryEntry de = GetDirectoryObject();
136
137 DirectorySearcher deSearch = new DirectorySearcher(de);
138
139 deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user)))";
140
141 deSearch.SearchScope = SearchScope.Subtree;
142
143
144
145 try
146 {
147 IList<string> list = new List<string>();
148 foreach (SearchResult result in deSearch.FindAll())
149 {
150
151 de = new DirectoryEntry(result.Path);
152 list.Add(GetProperty(de, "name"));
153 //list.Add(de.Name);
154
155 }
156 return list;
157 }
158
159 catch
160 {
161
162 return null;
163
164 }
165 }
166
167 #region GetDirectoryEntry
168
169
170
171 ///
172 ///根据用户公共名称取得用户的 对象
173 ///用户公共名称
174 ///如果找到该用户,则返回用户的 对象;否则返回 null
175
176 public static DirectoryEntry GetDirectoryEntry(string commonName)
177 {
178
179 DirectoryEntry de = GetDirectoryObject();
180
181 DirectorySearcher deSearch = new DirectorySearcher(de);
182
183 deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";
184
185 deSearch.SearchScope = SearchScope.Subtree;
186
187
188
189 try
190 {
191
192 SearchResult result = deSearch.FindOne();
193
194 de = new DirectoryEntry(result.Path);
195
196 return de;
197
198 }
199
200 catch
201 {
202
203 return null;
204
205 }
206
207 }
208
209
210
211 ///
212 ///根据用户公共名称和密码取得用户的 对象。
213 ///
214 ///用户公共名称
215 ///用户密码
216 ///如果找到该用户,则返回用户的 对象;否则返回 null
217
218 public static DirectoryEntry GetDirectoryEntry(string commonName, string password)
219 {
220
221 DirectoryEntry de = GetDirectoryObject(commonName, password);
222
223 DirectorySearcher deSearch = new DirectorySearcher(de);
224
225 deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";
226
227 deSearch.SearchScope = SearchScope.Subtree;
228
229
230
231 try
232 {
233
234 SearchResult result = deSearch.FindOne();
235
236 de = new DirectoryEntry(result.Path);
237
238 return de;
239
240 }
241
242 catch
243 {
244
245 return null;
246
247 }
248
249 }
250
251 ///
252
253 ///根据用户帐号称取得用户的 对象
254
255 ///
256
257 ///用户帐号名
258 ///如果找到该用户,则返回用户的 对象;否则返回 null
259
260 public static DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName)
261 {
262
263 DirectoryEntry de = GetDirectoryObject();
264
265 DirectorySearcher deSearch = new DirectorySearcher(de);
266
267 deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName=" + sAMAccountName + "))";
268
269 deSearch.SearchScope = SearchScope.Subtree;
270
271
272
273 try
274 {
275
276 SearchResult result = deSearch.FindOne();
277
278 de = new DirectoryEntry(result.Path);
279
280 return de;
281
282 }
283
284 catch
285 {
286
287 return null;
288
289 }
290
291 }
292
293 ///
294
295 ///根据用户帐号和密码取得用户的 对象
296
297 ///
298
299 ///用户帐号名
300 ///用户密码
301 ///如果找到该用户,则返回用户的 对象;否则返回 null
302
303 public static DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName, string password)
304 {
305
306 DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName);
307
308 if (de != null)
309 {
310
311 string commonName = de.Properties["cn"][0].ToString();
312
313
314
315 if (GetDirectoryEntry(commonName, password) != null)
316
317 return GetDirectoryEntry(commonName, password);
318
319 else
320
321 return null;
322
323 }
324
325 else
326 {
327
328 return null;
329
330 }
331
332 }
333
334
335
336 ///
337
338 ///根据组名取得用户组的 对象
339
340 ///
341
342 ///组名
343 ///
344
345 public static DirectoryEntry GetDirectoryEntryOfGroup(string groupName)
346 {
347
348 DirectoryEntry de = GetDirectoryObject();
349
350 DirectorySearcher deSearch = new DirectorySearcher(de);
351
352 deSearch.Filter = "(&(objectClass=group)(cn=" + groupName + "))";
353
354 deSearch.SearchScope = SearchScope.Subtree;
355
356
357
358 try
359 {
360
361 SearchResult result = deSearch.FindOne();
362
363 de = new DirectoryEntry(result.Path);
364
365 return de;
366
367 }
368
369 catch
370 {
371
372 return null;
373
374 }
375
376 }
377
378
379
380 #endregion
381
382
383
384 #region GetProperty
385
386
387
388 ///
389
390 ///获得指定 指定属性名对应的值
391
392 ///
393
394 ///
395
396 ///属性名称
397 ///属性值
398
399 public static string GetProperty(DirectoryEntry de, string propertyName)
400 {
401
402 if (de.Properties.Contains(propertyName))
403 {
404
405 return de.Properties[propertyName][0].ToString();
406
407 }
408
409 else
410 {
411
412 return string.Empty;
413
414 }
415
416 }
417
418
419
420 ///
421
422 ///获得指定搜索结果 中指定属性名对应的值
423
424 ///属性名称
425 ///属性值
426
427 public static string GetProperty(SearchResult searchResult, string propertyName)
428 {
429
430 if (searchResult.Properties.Contains(propertyName))
431 {
432
433 return searchResult.Properties[propertyName][0].ToString();
434
435 }
436
437 else
438 {
439
440 return string.Empty;
441
442 }
443
444 }
445
446
447
448 #endregion
449
450 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.DirectoryServices;
6 using System.Runtime.InteropServices;
7 using System.Security.Principal;
8 using System.Configuration;
9
10 public class ADHelper
11 {
12 ///
13
14 ///域名
15
16 ///
17
18 private static string DomainName = ConfigurationManager.AppSettings["DomainName"];
19
20 ///
21
22 /// LDAP绑定路径
23
24 ///
25
26 private static string ADPath = "LDAP://" + DomainName;
27
28 ///
29
30 ///登录帐号
31
32 ///
33
34 private static string ADUser = ConfigurationManager.AppSettings["ADUser"];
35 ///
36
37 ///登录密码
38
39 ///
40
41 private static string ADPassword = ConfigurationManager.AppSettings["ADPassword"];
42
43
44 /// <summary>
45 /// 用户属性定义
46 /// </summary>
47 public enum ADUserProperty
48 {
49 Name,//username
50 UserPrincipalName,//email
51 homePhone//tel
52 }
53
54 #region GetDirectoryObject
55
56 ///
57
58 ///获得DirectoryEntry对象实例,以管理员登陆AD
59
60 ///
61
62 ///
63
64 private static DirectoryEntry GetDirectoryObject()
65 {
66
67 DirectoryEntry entry = new DirectoryEntry(ADPath, ADUser, ADPassword, AuthenticationTypes.Secure);
68
69 return entry;
70
71 }
72
73
74
75 ///
76
77 ///根据指定用户名和密码获得相应DirectoryEntry实体
78
79 ///
80
81 private static DirectoryEntry GetDirectoryObject(string userName, string password)
82 {
83
84 DirectoryEntry entry = new DirectoryEntry(ADPath, userName, password, AuthenticationTypes.None);
85
86 return entry;
87
88 }
89
90
91
92 ///
93
94 /// i.e. /CN=Users,DC=creditsights, DC=cyberelves, DC=Com
95
96 ///
97
98 ///
99
100 ///
101
102 private static DirectoryEntry GetDirectoryObject(string domainReference)
103 {
104
105 DirectoryEntry entry = new DirectoryEntry(ADPath + domainReference, ADUser, ADPassword, AuthenticationTypes.Secure);
106
107 return entry;
108
109 }
110
111
112
113 ///
114
115 ///获得以UserName,Password创建的DirectoryEntry
116
117 ///
118
119 private static DirectoryEntry GetDirectoryObject(string domainReference, string userName, string password)
120 {
121
122 DirectoryEntry entry = new DirectoryEntry(ADPath + domainReference, userName, password, AuthenticationTypes.Secure);
123
124 return entry;
125
126 }
127
128
129
130 #endregion
131
132
133 public static IList<string> GetAllUserName()
134 {
135 DirectoryEntry de = GetDirectoryObject();
136
137 DirectorySearcher deSearch = new DirectorySearcher(de);
138
139 deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user)))";
140
141 deSearch.SearchScope = SearchScope.Subtree;
142
143
144
145 try
146 {
147 IList<string> list = new List<string>();
148 foreach (SearchResult result in deSearch.FindAll())
149 {
150
151 de = new DirectoryEntry(result.Path);
152 list.Add(GetProperty(de, "name"));
153 //list.Add(de.Name);
154
155 }
156 return list;
157 }
158
159 catch
160 {
161
162 return null;
163
164 }
165 }
166
167 #region GetDirectoryEntry
168
169
170
171 ///
172 ///根据用户公共名称取得用户的 对象
173 ///用户公共名称
174 ///如果找到该用户,则返回用户的 对象;否则返回 null
175
176 public static DirectoryEntry GetDirectoryEntry(string commonName)
177 {
178
179 DirectoryEntry de = GetDirectoryObject();
180
181 DirectorySearcher deSearch = new DirectorySearcher(de);
182
183 deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";
184
185 deSearch.SearchScope = SearchScope.Subtree;
186
187
188
189 try
190 {
191
192 SearchResult result = deSearch.FindOne();
193
194 de = new DirectoryEntry(result.Path);
195
196 return de;
197
198 }
199
200 catch
201 {
202
203 return null;
204
205 }
206
207 }
208
209
210
211 ///
212 ///根据用户公共名称和密码取得用户的 对象。
213 ///
214 ///用户公共名称
215 ///用户密码
216 ///如果找到该用户,则返回用户的 对象;否则返回 null
217
218 public static DirectoryEntry GetDirectoryEntry(string commonName, string password)
219 {
220
221 DirectoryEntry de = GetDirectoryObject(commonName, password);
222
223 DirectorySearcher deSearch = new DirectorySearcher(de);
224
225 deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";
226
227 deSearch.SearchScope = SearchScope.Subtree;
228
229
230
231 try
232 {
233
234 SearchResult result = deSearch.FindOne();
235
236 de = new DirectoryEntry(result.Path);
237
238 return de;
239
240 }
241
242 catch
243 {
244
245 return null;
246
247 }
248
249 }
250
251 ///
252
253 ///根据用户帐号称取得用户的 对象
254
255 ///
256
257 ///用户帐号名
258 ///如果找到该用户,则返回用户的 对象;否则返回 null
259
260 public static DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName)
261 {
262
263 DirectoryEntry de = GetDirectoryObject();
264
265 DirectorySearcher deSearch = new DirectorySearcher(de);
266
267 deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName=" + sAMAccountName + "))";
268
269 deSearch.SearchScope = SearchScope.Subtree;
270
271
272
273 try
274 {
275
276 SearchResult result = deSearch.FindOne();
277
278 de = new DirectoryEntry(result.Path);
279
280 return de;
281
282 }
283
284 catch
285 {
286
287 return null;
288
289 }
290
291 }
292
293 ///
294
295 ///根据用户帐号和密码取得用户的 对象
296
297 ///
298
299 ///用户帐号名
300 ///用户密码
301 ///如果找到该用户,则返回用户的 对象;否则返回 null
302
303 public static DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName, string password)
304 {
305
306 DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName);
307
308 if (de != null)
309 {
310
311 string commonName = de.Properties["cn"][0].ToString();
312
313
314
315 if (GetDirectoryEntry(commonName, password) != null)
316
317 return GetDirectoryEntry(commonName, password);
318
319 else
320
321 return null;
322
323 }
324
325 else
326 {
327
328 return null;
329
330 }
331
332 }
333
334
335
336 ///
337
338 ///根据组名取得用户组的 对象
339
340 ///
341
342 ///组名
343 ///
344
345 public static DirectoryEntry GetDirectoryEntryOfGroup(string groupName)
346 {
347
348 DirectoryEntry de = GetDirectoryObject();
349
350 DirectorySearcher deSearch = new DirectorySearcher(de);
351
352 deSearch.Filter = "(&(objectClass=group)(cn=" + groupName + "))";
353
354 deSearch.SearchScope = SearchScope.Subtree;
355
356
357
358 try
359 {
360
361 SearchResult result = deSearch.FindOne();
362
363 de = new DirectoryEntry(result.Path);
364
365 return de;
366
367 }
368
369 catch
370 {
371
372 return null;
373
374 }
375
376 }
377
378
379
380 #endregion
381
382
383
384 #region GetProperty
385
386
387
388 ///
389
390 ///获得指定 指定属性名对应的值
391
392 ///
393
394 ///
395
396 ///属性名称
397 ///属性值
398
399 public static string GetProperty(DirectoryEntry de, string propertyName)
400 {
401
402 if (de.Properties.Contains(propertyName))
403 {
404
405 return de.Properties[propertyName][0].ToString();
406
407 }
408
409 else
410 {
411
412 return string.Empty;
413
414 }
415
416 }
417
418
419
420 ///
421
422 ///获得指定搜索结果 中指定属性名对应的值
423
424 ///属性名称
425 ///属性值
426
427 public static string GetProperty(SearchResult searchResult, string propertyName)
428 {
429
430 if (searchResult.Properties.Contains(propertyName))
431 {
432
433 return searchResult.Properties[propertyName][0].ToString();
434
435 }
436
437 else
438 {
439
440 return string.Empty;
441
442 }
443
444 }
445
446
447
448 #endregion
449
450 }
只需要在配置文件中写入域名,管理员帐号和密码就OK了
调用程序如下:
1 string name = "lisa";
2 //DirectoryEntry en = ADHelper.GetDirectoryEntryByAccount(name);
3 DirectoryEntry en = ADHelper.GetDirectoryEntry(name);
4 Console.WriteLine(en.Name);
5 string email = ADHelper.GetProperty(en, ADHelper.ADUserProperty.UserPrincipalName.ToString());
6 string contact = ADHelper.GetProperty(en, "homePhone");
7
8 foreach (string user in ADHelper.GetAllUserName())
9 {
10
11 }
2 //DirectoryEntry en = ADHelper.GetDirectoryEntryByAccount(name);
3 DirectoryEntry en = ADHelper.GetDirectoryEntry(name);
4 Console.WriteLine(en.Name);
5 string email = ADHelper.GetProperty(en, ADHelper.ADUserProperty.UserPrincipalName.ToString());
6 string contact = ADHelper.GetProperty(en, "homePhone");
7
8 foreach (string user in ADHelper.GetAllUserName())
9 {
10
11 }