分享

AD相关信息

 实力决定地位 2013-08-21

domainName 域服務器名稱 例如 AD1.Domain.com.tw

ADPATH = LDAP://AD1.Domain.com.cn/DC= Domain,DC=com,DC=cn

private staticDirectoryEntry GetDirectoryObject(string domainName, stringadUser, string adPassword)

{

DirectoryEntry entry = newDirectoryEntry("LDAP://ad服務器名稱/DC=xxx,DC=com,DC=cn", adUser, adPassword, AuthenticationTypes.Secure);

return entry;

}

public static DirectoryEntry GetDirectoryEntry(string domainName, stringcommonName, string password)

{

try

{

DirectoryEntry de = GetDirectoryObject(domainName, commonName, password);

DirectorySearcherdeSearch = new DirectorySearcher(de);

deSearch.Filter = "(SAMAccountName="+ commonName + ")";

deSearch.SearchScope = SearchScope.Subtree;

SearchResult result = deSearch.FindOne();

de = new DirectoryEntry(result.Path);

return de;

}

catch(Exceptione)

{

return null;

}

}


  1. 原來是LDAP要加上AD電腦名稱 DirectoryEntry objDirEnt = new DirectoryEntry("LDAP://AD1.Domain.com.tw/CN=王小明,OU=客服部,DC=Domain,DC=com,DC=cn","帳號","密碼");


    AD1.Domain.com.tw 域服務器名稱



  2. using System;
  3. using System.DirectoryServices;
  4. namespace SystemFrameworks.Helper
  5. {
  6. ///
  7. ///活动目录辅助类。封装一系列活动目录操作相关的方法。
  8. ///
  9. public sealed class ADHelper
  10. {
  11. ///
  12. ///域名
  13. ///
  14. private static string DomainName = "MyDomain";
  15. ///
  16. /// LDAP 地址
  17. ///
  18. private static string LDAPDomain = "DC=MyDomain,DC=local";
  19. ///
  20. /// LDAP绑定路径
  21. ///
  22. private static string ADPath = "LDAP://brooks.mydomain.local";
  23. ///
  24. ///登录账号
  25. ///
  26. private static string ADUser = "Administrator";
  27. ///
  28. ///登录密码
  29. ///
  30. private static string ADPassword = "password";
  31. ///
  32. ///扮演类实例
  33. ///
  34. private static IdentityImpersonation impersonate = new IdentityImpersonation(ADUser, ADPassword, DomainName);
  35. ///
  36. ///用户登录验证结果
  37. ///
  38. public enum LoginResult
  39. {
  40. ///
  41. ///正常登录
  42. ///
  43. LOGIN_USER_OK = 0,
  44. ///
  45. ///用户不存在
  46. ///
  47. LOGIN_USER_DOESNT_EXIST,
  48. ///
  49. ///用户账号被禁用 t;
  50. ///
  51. LOGIN_USER_ACCOUNT_INACTIVE,
  52. ///
  53. ///用户密码不正确
  54. ///
  55. LOGIN_USER_PASSWORD_INCORRECT
  56. }
  57. ///
  58. ///用户属性定义标志
  59. ///
  60. public enum ADS_USER_FLAG_ENUM
  61. {
  62. ///
  63. ///登录脚本标志。如果通过 ADSI LDAP 进行读或写操作时,该标志失效。如果通过 ADSI WINNT,该标志为只读。
  64. ///
  65. ADS_UF_SCRIPT = 0X0001,
  66. ///
  67. ///用户账号禁用标志
  68. ///
  69. ADS_UF_ACCOUNTDISABLE = 0X0002,
  70. ///
  71. ///主文件夹标志
  72. ///
  73. ADS_UF_HOMEDIR_REQUIRED = 0X0008,
  74. ///
  75. ///过期标志
  76. ///
  77. ADS_UF_LOCKOUT = 0X0010,
  78. ///
  79. ///用户密码不是必须的
  80. ///
  81. ADS_UF_PASSWD_NOTREQD = 0X0020,
  82. ///
  83. ///密码不能更改标志
  84. ///
  85. ADS_UF_PASSWD_CANT_CHANGE = 0X0040,
  86. ///
  87. ///使用可逆的加密保存密码
  88. ///
  89. ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 0X0080,
  90. ///
  91. ///本地账号标志
  92. ///
  93. ADS_UF_TEMP_DUPLICATE_ACCOUNT = 0X0100,
  94. ///
  95. ///普通用户的默认账号类型
  96. ///
  97. ADS_UF_NORMAL_ACCOUNT = 0X0200,
  98. ///
  99. ///跨域的信任账号标志
  100. ///
  101. ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 0X0800,
  102. ///
  103. ///工作站信任账号标志
  104. ///
  105. ADS_UF_WORKSTATION_TRUST_ACCOUNT = 0x1000,
  106. ///
  107. ///服务器信任账号标志
  108. ///
  109. ADS_UF_SERVER_TRUST_ACCOUNT = 0X2000,
  110. ///
  111. ///密码永不过期标志
  112. ///
  113. ADS_UF_DONT_EXPIRE_PASSWD = 0X10000,
  114. ///
  115. /// MNS 账号标志
  116. ///
  117. ADS_UF_MNS_LOGON_ACCOUNT = 0X20000,
  118. ///
  119. ///交互式登录必须使用智能卡
  120. ///
  121. ADS_UF_SMARTCARD_REQUIRED = 0X40000,
  122. ///
  123. ///当设置该标志时,服务账号(用户或计算机账号)将通过 Kerberos 委托信任
  124. ///
  125. ADS_UF_TRUSTED_FOR_DELEGATION = 0X80000,
  126. ///
  127. ///当设置该标志时,即使服务账号是通过 Kerberos 委托信任的,敏感账号不能被委托
  128. ///
  129. ADS_UF_NOT_DELEGATED = 0X100000,
  130. ///
  131. ///此账号需要 DES 加密类型
  132. ///
  133. ADS_UF_USE_DES_KEY_ONLY = 0X200000,
  134. ///
  135. ///不要进行 Kerberos 预身份验证
  136. ///
  137. ADS_UF_DONT_REQUIRE_PREAUTH = 0X4000000,
  138. ///
  139. ///用户密码过期标志
  140. ///
  141. ADS_UF_PASSWORD_EXPIRED = 0X800000,
  142. ///
  143. ///用户账号可委托标志
  144. ///
  145. ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 0X1000000
  146. }
  147. public ADHelper()
  148. {
  149. //
  150. }
  151. #region GetDirectoryObject
  152. ///
  153. ///获得DirectoryEntry对象实例,以管理员登陆AD
  154. ///
  155. ///
  156. private static DirectoryEntry GetDirectoryObject()
  157. {
  158. DirectoryEntry entry = new DirectoryEntry(ADPath, ADUser, ADPassword, AuthenticationTypes.Secure);
  159. return entry;
  160. }
  161. ///
  162. ///根据指定用户名和密码获得相应DirectoryEntry实体
  163. ///
  164. ///
  165. ///
  166. ///
  167. private static DirectoryEntry GetDirectoryObject(string userName, string password)
  168. {
  169. DirectoryEntry entry = new DirectoryEntry(ADPath, userName, password, AuthenticationTypes.None);
  170. return entry;
  171. }
  172. ///
  173. /// i.e. /CN=Users,DC=creditsights, DC=cyberelves, DC=Com
  174. ///
  175. ///
  176. ///
  177. private static DirectoryEntry GetDirectoryObject(string domainReference)
  178. {
  179. DirectoryEntry entry = new DirectoryEntry(ADPath + domainReference, ADUser, ADPassword, AuthenticationTypes.Secure);
  180. return entry;
  181. }
  182. ///
  183. ///获得以UserName,Password创建的DirectoryEntry
  184. ///
  185. ///
  186. ///
  187. ///
  188. ///
  189. private static DirectoryEntry GetDirectoryObject(string domainReference, string userName, string password)
  190. {
  191. DirectoryEntry entry = new DirectoryEntry(ADPath + domainReference, userName, password, AuthenticationTypes.Secure);
  192. return entry;
  193. }
  194. #endregion
  195. #region GetDirectoryEntry
  196. ///
  197. ///根据用户公共名称取得用户的 对象
  198. ///
  199. ///
  200. 用户公共名称
  201. ///如果找到该用户,则返回用户的 对象;否则返回 null
  202. public static DirectoryEntry GetDirectoryEntry(string commonName)
  203. {
  204. DirectoryEntry de = GetDirectoryObject();
  205. DirectorySearcher deSearch = new DirectorySearcher(de);
  206. deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";
  207. deSearch.SearchScope = SearchScope.Subtree;
  208. try
  209. {
  210. SearchResult result = deSearch.FindOne();
  211. de = new DirectoryEntry(result.Path);
  212. return de;
  213. }
  214. catch
  215. {
  216. return null;
  217. }
  218. }
  219. ///
  220. ///根据用户公共名称和密码取得用户的 对象。
  221. ///
  222. ///
  223. 用户公共名称
  224. ///
  225. 用户密码
  226. ///如果找到该用户,则返回用户的 对象;否则返回 null
  227. public static DirectoryEntry GetDirectoryEntry(string commonName, string password)
  228. {
  229. DirectoryEntry de = GetDirectoryObject(commonName, password);
  230. DirectorySearcher deSearch = new DirectorySearcher(de);
  231. deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";
  232. deSearch.SearchScope = SearchScope.Subtree;
  233. try
  234. {
  235. SearchResult result = deSearch.FindOne();
  236. de = new DirectoryEntry(result.Path);
  237. return de;
  238. }
  239. catch
  240. {
  241. return null;
  242. }
  243. }
  244. ///
  245. ///根据用户账号称取得用户的 对象
  246. ///
  247. ///
  248. 用户账号名
  249. ///如果找到该用户,则返回用户的 对象;否则返回 null
  250. public static DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName)
  251. {
  252. DirectoryEntry de = GetDirectoryObject();
  253. DirectorySearcher deSearch = new DirectorySearcher(de);
  254. deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName=" + sAMAccountName + "))";
  255. deSearch.SearchScope = SearchScope.Subtree;
  256. try
  257. {
  258. SearchResult result = deSearch.FindOne();
  259. de = new DirectoryEntry(result.Path);
  260. return de;
  261. }
  262. catch
  263. {
  264. return null;
  265. }
  266. }
  267. ///
  268. ///根据用户账号和密码取得用户的 对象
  269. ///
  270. ///
  271. 用户账号名
  272. ///
  273. 用户密码
  274. ///如果找到该用户,则返回用户的 对象;否则返回 null
  275. public static DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName, string password)
  276. {
  277. DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName);
  278. if (de != null)
  279. {
  280. string commonName = de.Properties["cn"][0].ToString();
  281. if (GetDirectoryEntry(commonName, password) != null)
  282. return GetDirectoryEntry(commonName, password);
  283. else
  284. return null;
  285. }
  286. else
  287. {
  288. return null;
  289. }
  290. }
  291. ///
  292. ///根据组名取得用户组的 对象
  293. ///
  294. ///
  295. 组名
  296. ///
  297. public static DirectoryEntry GetDirectoryEntryOfGroup(string groupName)
  298. {
  299. DirectoryEntry de = GetDirectoryObject();
  300. DirectorySearcher deSearch = new DirectorySearcher(de);
  301. deSearch.Filter = "(&(objectClass=group)(cn=" + groupName + "))";
  302. deSearch.SearchScope = SearchScope.Subtree;
  303. try
  304. {
  305. SearchResult result = deSearch.FindOne();
  306. de = new DirectoryEntry(result.Path);
  307. return de;
  308. }
  309. catch
  310. {
  311. return null;
  312. }
  313. }
  314. #endregion
  315. #region GetProperty
  316. ///
  317. ///获得指定 指定属性名对应的值
  318. ///
  319. ///
  320. ///
  321. 属性名称
  322. ///属性值
  323. public static string GetProperty(DirectoryEntry de, string propertyName)
  324. {
  325. if(de.Properties.Contains(propertyName))
  326. {
  327. return de.Properties[propertyName][0].ToString() ;
  328. }
  329. else
  330. {
  331. return string.Empty;
  332. }
  333. }
  334. ///
  335. ///获得指定搜索结果 中指定属性名对应的值
  336. ///
  337. ///
  338. ///
  339. 属性名称
  340. ///属性值
  341. public static string GetProperty(SearchResult searchResult, string propertyName)
  342. {
  343. if(searchResult.Properties.Contains(propertyName))
  344. {
  345. return searchResult.Properties[propertyName][0].ToString() ;
  346. }
  347. else
  348. {
  349. return string.Empty;
  350. }
  351. }
  352. #endregion
  353. ///
  354. ///设置指定 的属性值
  355. ///
  356. ///
  357. ///
  358. 属性名称
  359. ///
  360. 属性值
  361. public static void SetProperty(DirectoryEntry de, string propertyName, string propertyValue)
  362. {
  363. if(propertyValue != string.Empty || propertyValue != "" || propertyValue != null)
  364. {
  365. if(de.Properties.Contains(propertyName))
  366. {
  367. de.Properties[propertyName][0] = propertyValue;
  368. }
  369. else
  370. {
  371. de.Properties[propertyName].Add(propertyValue);
  372. }
  373. }
  374. }
  375. ///
  376. ///创建新的用户
  377. ///
  378. ///
  379. DN 位置。例如:OU=共享平台 或 CN=Users
  380. ///
  381. 公共名称
  382. ///
  383. 账号
  384. ///
  385. 密码
  386. ///
  387. public static DirectoryEntry CreateNewUser(string ldapDN, string commonName, string sAMAccountName, string password)
  388. {
  389. DirectoryEntry entry = GetDirectoryObject();
  390. DirectoryEntry subEntry = entry.Children.Find(ldapDN);
  391. DirectoryEntry deUser = subEntry.Children.Add("CN=" + commonName, "user");
  392. deUser.Properties["sAMAccountName"].Value = sAMAccountName;
  393. deUser.CommitChanges();
  394. ADHelper.EnableUser(commonName);
  395. ADHelper.SetPassword(commonName, password);
  396. deUser.Close();
  397. return deUser;
  398. }
  399. ///
  400. ///创建新的用户。默认创建在 Users 单元下。
  401. ///
  402. ///
  403. 公共名称
  404. ///
  405. 账号
  406. ///
  407. 密码
  408. ///
  409. public static DirectoryEntry CreateNewUser(string commonName, string sAMAccountName, string password)
  410. {
  411. return CreateNewUser("CN=Users", commonName, sAMAccountName, password);
  412. }
  413. ///
  414. ///判断指定公共名称的用户是否存在
  415. ///
  416. ///
  417. 用户公共名称
  418. ///如果存在,返回 true;否则返回 false
  419. public static bool IsUserExists(string commonName)
  420. {
  421. DirectoryEntry de = GetDirectoryObject();
  422. DirectorySearcher deSearch = new DirectorySearcher(de);
  423. deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))"; // LDAP 查询串
  424. SearchResultCollection results = deSearch.FindAll();
  425. if (results.Count == 0)
  426. return false;
  427. else
  428. return true;
  429. }
  430. ///
  431. ///判断用户账号是否激活
  432. ///
  433. ///
  434. 用户账号属性控制器
  435. ///如果用户账号已经激活,返回 true;否则返回 false
  436. public static bool IsAccountActive(int userAccountControl)
  437. {
  438. int userAccountControl_Disabled = Convert.ToInt32(ADS_USER_FLAG_ENUM.ADS_UF_ACCOUNTDISABLE);
  439. int flagExists = userAccountControl & userAccountControl_Disabled;
  440. if (flagExists > 0)
  441. return false;
  442. else
  443. return true;
  444. }
  445. ///
  446. ///判断用户与密码是否足够以满足身份验证进而登录
  447. ///
  448. ///
  449. 用户公共名称
  450. ///
  451. 密码
  452. ///如能可正常登录,则返回 true;否则返回 false
  453. public static LoginResult Login(string commonName, string password)
  454. {
  455. DirectoryEntry de = GetDirectoryEntry(commonName);
  456. if (de != null)
  457. {
  458. // 必须在判断用户密码正确前,对账号激活属性进行判断;否则将出现异常。
  459. int userAccountControl = Convert.ToInt32(de.Properties["userAccountControl"][0]);
  460. de.Close();
  461. if (!IsAccountActive(userAccountControl))
  462. return LoginResult.LOGIN_USER_ACCOUNT_INACTIVE;
  463. if (GetDirectoryEntry(commonName, password) != null)
  464. return LoginResult.LOGIN_USER_OK;
  465. else
  466. return LoginResult.LOGIN_USER_PASSWORD_INCORRECT;
  467. }
  468. else
  469. {
  470. return LoginResult.LOGIN_USER_DOESNT_EXIST;
  471. }
  472. }
  473. ///
  474. ///判断用户账号与密码是否足够以满足身份验证进而登录
  475. ///
  476. ///
  477. 用户账号
  478. ///
  479. 密码
  480. ///如能可正常登录,则返回 true;否则返回 false
  481. public static LoginResult LoginByAccount(string sAMAccountName, string password)
  482. {
  483. DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName);
  484. if (de != null)
  485. {
  486. // 必须在判断用户密码正确前,对账号激活属性进行判断;否则将出现异常。
  487. int userAccountControl = Convert.ToInt32(de.Properties["userAccountControl"][0]);
  488. de.Close();
  489. if (!IsAccountActive(userAccountControl))
  490. return LoginResult.LOGIN_USER_ACCOUNT_INACTIVE;
  491. if (GetDirectoryEntryByAccount(sAMAccountName, password) != null)
  492. return LoginResult.LOGIN_USER_OK;
  493. else
  494. return LoginResult.LOGIN_USER_PASSWORD_INCORRECT;
  495. }
  496. else
  497. {
  498. return LoginResult.LOGIN_USER_DOESNT_EXIST;
  499. }
  500. }
  501. ///
  502. ///设置用户密码,管理员可以通过它来修改指定用户的密码。
  503. ///
  504. ///
  505. 用户公共名称
  506. ///
  507. 用户新密码
  508. public static void SetPassword(string commonName, string newPassword)
  509. {
  510. DirectoryEntry de = GetDirectoryEntry(commonName);
  511. // 模拟超级管理员,以达到有权限修改用户密码
  512. impersonate.BeginImpersonate();
  513. de.Invoke("SetPassword", new object[]{newPassword});
  514. impersonate.StopImpersonate();
  515. de.Close();
  516. }
  517. ///
  518. ///设置账号密码,管理员可以通过它来修改指定账号的密码。
  519. ///
  520. ///
  521. 用户账号
  522. ///
  523. 用户新密码
  524. public static void SetPasswordByAccount(string sAMAccountName, string newPassword)
  525. {
  526. DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName);
  527. // 模拟超级管理员,以达到有权限修改用户密码
  528. IdentityImpersonation impersonate = new IdentityImpersonation(ADUser, ADPassword, DomainName);
  529. impersonate.BeginImpersonate();
  530. de.Invoke("SetPassword", new object[]{newPassword});
  531. impersonate.StopImpersonate();
  532. de.Close();
  533. }
  534. ///
  535. ///修改用户密码
  536. ///
  537. ///
  538. 用户公共名称
  539. ///
  540. 旧密码
  541. ///
  542. 新密码
  543. public static void ChangeUserPassword (string commonName, string oldPassword, string newPassword)
  544. {
  545. // to-do: 需要解决密码策略问题
  546. DirectoryEntry oUser = GetDirectoryEntry(commonName);
  547. oUser.Invoke("ChangePassword", new Object[]{oldPassword, newPassword});
  548. oUser.Close();
  549. }
  550. ///
  551. ///启用指定公共名称的用户
  552. ///
  553. ///
  554. 用户公共名称
  555. public static void EnableUser(string commonName)
  556. {
  557. EnableUser(GetDirectoryEntry(commonName));
  558. }
  559. ///
  560. ///启用指定 的用户
  561. ///
  562. ///
  563. public static void EnableUser(DirectoryEntry de)
  564. {
  565. impersonate.BeginImpersonate();
  566. de.Properties["userAccountControl"][0] = ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_NORMAL_ACCOUNT | ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_DONT_EXPIRE_PASSWD;
  567. de.CommitChanges();
  568. impersonate.StopImpersonate();
  569. de.Close();
  570. }
  571. ///
  572. ///禁用指定公共名称的用户
  573. ///
  574. ///
  575. 用户公共名称
  576. public static void DisableUser(string commonName)
  577. {
  578. DisableUser(GetDirectoryEntry(commonName));
  579. }
  580. ///
  581. ///禁用指定 的用户
  582. ///
  583. ///
  584. public static void DisableUser(DirectoryEntry de)
  585. {
  586. impersonate.BeginImpersonate();
  587. de.Properties["userAccountControl"][0]=ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_NORMAL_ACCOUNT | ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_DONT_EXPIRE_PASSWD | ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_ACCOUNTDISABLE;
  588. de.CommitChanges();
  589. impersonate.StopImpersonate();
  590. de.Close();
  591. }
  592. ///
  593. ///将指定的用户添加到指定的组中。默认为 Users 下的组和用户。
  594. ///
  595. ///
  596. 用户公共名称
  597. ///
  598. 组名
  599. public static void AddUserToGroup(string userCommonName, string groupName)
  600. {
  601. DirectoryEntry oGroup = GetDirectoryEntryOfGroup(groupName);
  602. DirectoryEntry oUser = GetDirectoryEntry(userCommonName);
  603. impersonate.BeginImpersonate();
  604. oGroup.Properties["member"].Add(oUser.Properties["distinguishedName"].Value);
  605. oGroup.CommitChanges();
  606. impersonate.StopImpersonate();
  607. oGroup.Close();
  608. oUser.Close();
  609. }
  610. ///
  611. ///将用户从指定组中移除。默认为 Users 下的组和用户。
  612. ///
  613. ///
  614. 用户公共名称
  615. ///
  616. 组名
  617. public static void RemoveUserFromGroup(string userCommonName, string groupName)
  618. {
  619. DirectoryEntry oGroup = GetDirectoryEntryOfGroup(groupName);
  620. DirectoryEntry oUser = GetDirectoryEntry(userCommonName);
  621. impersonate.BeginImpersonate();
  622. oGroup.Properties["member"].Remove(oUser.Properties["distinguishedName"].Value);
  623. oGroup.CommitChanges();
  624. impersonate.StopImpersonate();
  625. oGroup.Close();
  626. oUser.Close();
  627. }
  628. }
  629. ///
  630. ///用户模拟角色类。实现在程序段内进行用户角色模拟。
  631. ///
  632. public class IdentityImpersonation
  633. {
  634. [DllImport("advapi32.dll", SetLastError=true)]
  635. public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
  636. [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  637. public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
  638. [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
  639. public extern static bool CloseHandle(IntPtr handle);
  640. // 要模拟的用户的用户名、密码、域(机器名)
  641. private String _sImperUsername;
  642. private String _sImperPassword;
  643. private String _sImperDomain;
  644. // 记录模拟上下文
  645. private WindowsImpersonationContext _imperContext;
  646. private IntPtr _adminToken;
  647. private IntPtr _dupeToken;
  648. // 是否已停止模拟
  649. private Boolean _bClosed;
  650. ///
  651. ///构造函数
  652. ///
  653. ///
  654. 所要模拟的用户的用户名
  655. ///
  656. 所要模拟的用户的密码
  657. ///
  658. 所要模拟的用户所在的域
  659. public IdentityImpersonation(String impersonationUsername, String impersonationPassword, String impersonationDomain)
  660. {
  661. _sImperUsername = impersonationUsername;
  662. _sImperPassword = impersonationPassword;
  663. _sImperDomain = impersonationDomain;
  664. _adminToken = IntPtr.Zero;
  665. _dupeToken = IntPtr.Zero;
  666. _bClosed = true;
  667. }
  668. ///
  669. ///析构函数
  670. ///
  671. ~IdentityImpersonation()
  672. {
  673. if(!_bClosed)
  674. {
  675. StopImpersonate();
  676. }
  677. }
  678. ///
  679. ///开始身份角色模拟。
  680. ///
  681. ///
  682. public Boolean BeginImpersonate()
  683. {
  684. Boolean bLogined = LogonUser(_sImperUsername, _sImperDomain, _sImperPassword, 2, 0, ref _adminToken);
  685. if(!bLogined)
  686. {
  687. return false;
  688. }
  689. Boolean bDuped = DuplicateToken(_adminToken, 2, ref _dupeToken);
  690. if(!bDuped)
  691. {
  692. return false;
  693. }
  694. WindowsIdentity fakeId = new WindowsIdentity(_dupeToken);
  695. _imperContext = fakeId.Impersonate();
  696. _bClosed = false;
  697. return true;
  698. }
  699. ///
  700. ///停止身分角色模拟。
  701. ///
  702. public void StopImpersonate()
  703. {
  704. _imperContext.Undo();
  705. CloseHandle(_dupeToken);
  706. CloseHandle(_adminToken);
  707. _bClosed = true;
  708. }
  709. }
  710. }
  711. =====================================================
  712. 简单的应用
  713. [WebMethod]
  714. public string IsAuthenticated(string UserID,string Password)
  715. {
  716. string _path = "LDAP://" + adm + "/DC=lamda,DC=com,DC=cn";//"LDAP://172.75.200.1/DC=名字,DC=com,DC=cn";
  717. string _filterAttribute=null;
  718. DirectoryEntry entry = new DirectoryEntry(_path,UserID,Password);
  719. try
  720. {
  721. //Bind to the native AdsObject to force authentication.
  722. DirectorySearcher search = new DirectorySearcher(entry);
  723. search.Filter = "(SAMAccountName=" + UserID + ")";
  724. SearchResult result = search.FindOne();
  725. if(null == result)
  726. {
  727. _filterAttribute="登录失败: 未知的用户名或错误密码.";
  728. }
  729. else
  730. {
  731. _filterAttribute="true";
  732. }
  733. }
  734. catch (Exception ex)
  735. {
  736. // if(ex.Message.StartsWith("该服务器不可操作"))
  737. // {
  738. // string mail = ADO.GetConnString("mail");
  739. // entry.Path = "LDAP://"+mail+"/OU=名字,DC=it2004,DC=gree,DC=com,DC=cn";
  740. // try
  741. // {
  742. // DirectorySearcher search = new DirectorySearcher(entry);
  743. // search.Filter = "(SAMAccountName=" + UserID + ")";
  744. // SearchResult result = search.FindOne();
  745. //
  746. // if(null == result)
  747. // {
  748. // _filterAttribute="登录失败: 未知的用户名或错误密码.";
  749. // }
  750. // else
  751. // {
  752. // _filterAttribute="true";
  753. // }
  754. // return _filterAttribute;
  755. //
  756. // }
  757. // catch (Exception ex1)
  758. // {
  759. // return ex1.Message;
  760. // }
  761. //
  762. // }
  763. // else
  764. return ex.Message;
  765. }
  766. return _filterAttribute;
  767. }
  768. [WebMethod]
  769. public string[] LDAPMessage(string UserID)
  770. {
  771. string _path = "LDAP://"+adm+"/DC=it2004,DC=名字,DC=com,DC=cn";
  772. string[] _filterAttribute=new string[5];
  773. string[] msg = {"samaccountname","displayname","department","company"};
  774. DirectoryEntry entry = new DirectoryEntry(_path,"180037","790813");
  775. try
  776. {
  777. Object obj = entry.NativeObject;
  778. DirectorySearcher search = new DirectorySearcher(entry);
  779. search.Filter = "(SAMAccountName=" + UserID + ")";
  780. SearchResult result = search.FindOne();
  781. if(null == result)
  782. {
  783. _filterAttribute[0]="登录失败: 未知的用户名或错误密码.";
  784. }
  785. else
  786. {
  787. _filterAttribute[0]="true";
  788. for(int propertyCounter = 1; propertyCounter < 5; propertyCounter++)
  789. {
  790. if(propertyCounter==4 && result.Properties[msg[propertyCounter-1]][0]==null)
  791. break;
  792. _filterAttribute[propertyCounter]=result.Properties[msg[propertyCounter-1]][0].ToString();
  793. }
  794. }
  795. }
  796. catch (Exception ex)
  797. {
  798. //_filterAttribute[0]=ex.Message;
  799. }
  800. return _filterAttribute;
  801. }
  802. [WebMethod]
  803. public string[] AllMembers()
  804. {
  805. string[] msg;
  806. string _path = "LDAP://名字";
  807. DirectoryEntry entry = new DirectoryEntry(_path,"180037","790813");
  808. //Bind to the native AdsObject to force authentication.
  809. Object obj = entry.NativeObject;
  810. System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
  811. mySearcher.Filter = "(SAMAccountName=180037)";
  812. msg=new string[mySearcher.FindAll().Count];
  813. int i=0;
  814. foreach(System.DirectoryServices.SearchResult result in mySearcher.FindAll())
  815. {
  816. msg[i++]=result.Path;
  817. }
  818. return msg;
  819. }
  820. }

//***********************************************************************

//***********************************************************************

//***********************************************************************

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.DirectoryServices;

namespace change

{

/// <summary>

/// Form1 的摘要说明。

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.ComboBox comboBox2;

private System.Windows.Forms.Label label3;

private System.Windows.Forms.ComboBox comboBox1;

private System.Windows.Forms.Label label2;

string str="";

string strErr="";

string strManager="";

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label4;

private System.Windows.Forms.Button button1;

private System.Windows.Forms.TextBox textBox1;

private System.Windows.Forms.TextBox textBox2;

private System.Windows.Forms.TextBox textBox3;

private System.Windows.Forms.CheckBox checkBox1;

private System.Windows.Forms.CheckBox checkBox2;

private System.Windows.Forms.CheckBox checkBox3;

private System.Windows.Forms.Label label5;

private System.Windows.Forms.TextBox textBox4;

private System.Windows.Forms.ComboBox comboBox3;

private System.Windows.Forms.Label label6;

private System.Windows.Forms.ComboBox comboBox4;

private System.Windows.Forms.Label label7;

private System.Windows.Forms.ComboBox comboBox5;

private System.Windows.Forms.Label label8;

private System.Windows.Forms.ComboBox comboBox6;

private System.Windows.Forms.Label label9;

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码

//

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.comboBox2 = new System.Windows.Forms.ComboBox();

this.label3 = new System.Windows.Forms.Label();

this.comboBox1 = new System.Windows.Forms.ComboBox();

this.label2 = new System.Windows.Forms.Label();

this.label1 = new System.Windows.Forms.Label();

this.label4 = new System.Windows.Forms.Label();

this.button1 = new System.Windows.Forms.Button();

this.textBox1 = new System.Windows.Forms.TextBox();

this.textBox2 = new System.Windows.Forms.TextBox();

this.textBox3 = new System.Windows.Forms.TextBox();

this.checkBox1 = new System.Windows.Forms.CheckBox();

this.checkBox2 = new System.Windows.Forms.CheckBox();

this.checkBox3 = new System.Windows.Forms.CheckBox();

this.label5 = new System.Windows.Forms.Label();

this.textBox4 = new System.Windows.Forms.TextBox();

this.comboBox3 = new System.Windows.Forms.ComboBox();

this.label6 = new System.Windows.Forms.Label();

this.comboBox4 = new System.Windows.Forms.ComboBox();

this.label7 = new System.Windows.Forms.Label();

this.comboBox5 = new System.Windows.Forms.ComboBox();

this.label8 = new System.Windows.Forms.Label();

this.comboBox6 = new System.Windows.Forms.ComboBox();

this.label9 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// comboBox2

//

this.comboBox2.Location = new System.Drawing.Point(416, 16);

this.comboBox2.Name = "comboBox2";

this.comboBox2.Size = new System.Drawing.Size(121, 20);

this.comboBox2.TabIndex = 10;

this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);

//

// label3

//

this.label3.Location = new System.Drawing.Point(320, 20);

this.label3.Name = "label3";

this.label3.Size = new System.Drawing.Size(80, 16);

this.label3.TabIndex = 9;

this.label3.Text = "选择部门(OU)";

//

// comboBox1

//

this.comboBox1.Location = new System.Drawing.Point(144, 16);

this.comboBox1.Name = "comboBox1";

this.comboBox1.Size = new System.Drawing.Size(121, 20);

this.comboBox1.TabIndex = 8;

this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

//

// label2

//

this.label2.AutoSize = true;

this.label2.Location = new System.Drawing.Point(40, 19);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(79, 17);

this.label2.TabIndex = 7;

this.label2.Text = "选择公司(OU)";

//

// label1

//

this.label1.Location = new System.Drawing.Point(48, 128);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(72, 23);

this.label1.TabIndex = 11;

this.label1.Text = "新公司名称";

//

// label4

//

this.label4.Location = new System.Drawing.Point(328, 128);

this.label4.Name = "label4";

this.label4.Size = new System.Drawing.Size(72, 23);

this.label4.TabIndex = 12;

this.label4.Text = "新部门名称";

//

// button1

//

this.button1.Location = new System.Drawing.Point(376, 376);

this.button1.Name = "button1";

this.button1.TabIndex = 15;

this.button1.Text = "开始处理";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(144, 128);

this.textBox1.Name = "textBox1";

this.textBox1.Size = new System.Drawing.Size(120, 21);

this.textBox1.TabIndex = 16;

this.textBox1.Text = "";

//

// textBox2

//

this.textBox2.Location = new System.Drawing.Point(416, 128);

this.textBox2.Name = "textBox2";

this.textBox2.Size = new System.Drawing.Size(120, 21);

this.textBox2.TabIndex = 17;

this.textBox2.Text = "";

//

// textBox3

//

this.textBox3.Location = new System.Drawing.Point(32, 232);

this.textBox3.Multiline = true;

this.textBox3.Name = "textBox3";

this.textBox3.ReadOnly = true;

this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Both;

this.textBox3.Size = new System.Drawing.Size(504, 120);

this.textBox3.TabIndex = 18;

this.textBox3.Text = "";

//

// checkBox1

//

this.checkBox1.Location = new System.Drawing.Point(64, 192);

this.checkBox1.Name = "checkBox1";

this.checkBox1.TabIndex = 20;

this.checkBox1.Text = "修改公司名称";

//

// checkBox2

//

this.checkBox2.Location = new System.Drawing.Point(200, 192);

this.checkBox2.Name = "checkBox2";

this.checkBox2.TabIndex = 21;

this.checkBox2.Text = "修改部门名称";

//

// checkBox3

//

this.checkBox3.Location = new System.Drawing.Point(336, 192);

this.checkBox3.Name = "checkBox3";

this.checkBox3.TabIndex = 22;

this.checkBox3.Text = "修改部门领导";

//

// label5

//

this.label5.AutoSize = true;

this.label5.Location = new System.Drawing.Point(40, 160);

this.label5.Name = "label5";

this.label5.Size = new System.Drawing.Size(79, 17);

this.label5.TabIndex = 19;

this.label5.Text = "新的部门领导";

//

// textBox4

//

this.textBox4.Location = new System.Drawing.Point(144, 160);

this.textBox4.Name = "textBox4";

this.textBox4.Size = new System.Drawing.Size(392, 21);

this.textBox4.TabIndex = 23;

this.textBox4.Text = "请输入新领导的姓名全拼(域账号)";

//

// comboBox3

//

this.comboBox3.Location = new System.Drawing.Point(144, 48);

this.comboBox3.Name = "comboBox3";

this.comboBox3.Size = new System.Drawing.Size(121, 20);

this.comboBox3.TabIndex = 25;

this.comboBox3.SelectedIndexChanged += new System.EventHandler(this.comboBox3_SelectedIndexChanged);

//

// label6

//

this.label6.AutoSize = true;

this.label6.Location = new System.Drawing.Point(4, 51);

this.label6.Name = "label6";

this.label6.Size = new System.Drawing.Size(116, 17);

this.label6.TabIndex = 24;

this.label6.Text = "选择二级子部门(OU)";

//

// comboBox4

//

this.comboBox4.Location = new System.Drawing.Point(416, 48);

this.comboBox4.Name = "comboBox4";

this.comboBox4.Size = new System.Drawing.Size(121, 20);

this.comboBox4.TabIndex = 27;

this.comboBox4.SelectedIndexChanged += new System.EventHandler(this.comboBox4_SelectedIndexChanged);

//

// label7

//

this.label7.AutoSize = true;

this.label7.Location = new System.Drawing.Point(284, 51);

this.label7.Name = "label7";

this.label7.Size = new System.Drawing.Size(116, 17);

this.label7.TabIndex = 26;

this.label7.Text = "选择三级子部门(OU)";

//

// comboBox5

//

this.comboBox5.Location = new System.Drawing.Point(416, 80);

this.comboBox5.Name = "comboBox5";

this.comboBox5.Size = new System.Drawing.Size(121, 20);

this.comboBox5.TabIndex = 31;

this.comboBox5.SelectedIndexChanged += new System.EventHandler(this.comboBox5_SelectedIndexChanged);

//

// label8

//

this.label8.Location = new System.Drawing.Point(280, 84);

this.label8.Name = "label8";

this.label8.Size = new System.Drawing.Size(120, 16);

this.label8.TabIndex = 30;

this.label8.Text = "选择级五子部门(OU)";

//

// comboBox6

//

this.comboBox6.Location = new System.Drawing.Point(144, 80);

this.comboBox6.Name = "comboBox6";

this.comboBox6.Size = new System.Drawing.Size(121, 20);

this.comboBox6.TabIndex = 29;

this.comboBox6.SelectedIndexChanged += new System.EventHandler(this.comboBox6_SelectedIndexChanged);

//

// label9

//

this.label9.AutoSize = true;

this.label9.Location = new System.Drawing.Point(4, 83);

this.label9.Name = "label9";

this.label9.Size = new System.Drawing.Size(116, 17);

this.label9.TabIndex = 28;

this.label9.Text = "选择四级子部门(OU)";

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(584, 429);

this.Controls.Add(this.comboBox5);

this.Controls.Add(this.label8);

this.Controls.Add(this.comboBox6);

this.Controls.Add(this.label9);

this.Controls.Add(this.comboBox4);

this.Controls.Add(this.label7);

this.Controls.Add(this.comboBox3);

this.Controls.Add(this.label6);

this.Controls.Add(this.textBox4);

this.Controls.Add(this.checkBox3);

this.Controls.Add(this.checkBox2);

this.Controls.Add(this.checkBox1);

this.Controls.Add(this.label5);

this.Controls.Add(this.textBox3);

this.Controls.Add(this.textBox2);

this.Controls.Add(this.textBox1);

this.Controls.Add(this.button1);

this.Controls.Add(this.label4);

this.Controls.Add(this.label1);

this.Controls.Add(this.comboBox2);

this.Controls.Add(this.label3);

this.Controls.Add(this.comboBox1);

this.Controls.Add(this.label2);

this.Name = "Form1";

this.Text = "按OU成批修改信息";

this.Load += new System.EventHandler(this.Form1_Load);

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)

{

//str=listBox1.SelectedItem.ToString();

str=comboBox1.SelectedItem.ToString();

DirectoryEntry de1=new DirectoryEntry();

de1.Path="LDAP://"+str+",DC=test,DC=net";

try

{

comboBox2.Items.Clear();

comboBox2.Text="";

comboBox2.Refresh();

foreach (DirectoryEntry ch1 in de1.Children)

{

// textBox1.Text=textBox1.Text+str+"/r/n";//ch.Properties["adpath"][0].ToString();

string str1="";

str1=ch1.SchemaClassName.ToString();

if (str1=="organizationalUnit")

{

comboBox2.Items.Add(ch1.Name.ToString());

}

}

de1.Close();

//textBox1.Text=textBox1.Text+"--------------next------------------------/r/n";

// MessageBox.Show("finish!!!");

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

finally

{}

}

private void Form1_Load(object sender, System.EventArgs e)

{

//初始化公司选择框

DirectoryEntry de1=new DirectoryEntry();

de1.Path="LDAP://DC=test,DC=net";

try

{

foreach (DirectoryEntry ch1 in de1.Children)

{

// textBox1.Text=textBox1.Text+ch1.Name+"/r/n";//ch.Properties["adpath"][0].ToString();

str=ch1.Name.ToString();

string str1="";

//str1=str.Substring(0,str.IndexOf("="));

str1=ch1.SchemaClassName.ToString();

if (str1=="organizationalUnit")

{

//listBox1.Items.Add(ch1.Name.ToString());

comboBox1.Items.Add(ch1.Name.ToString());

// comboBox3.Items.Add(ch1.Name.ToString());

}

}

de1.Close();

//textBox1.Text=textBox1.Text+"--------------next------------------------/r/n";

// MessageBox.Show("finish!!!");

}

catch(Exception ex)

{

strErr=ex.Message;

}

finally

{}

}

private void button1_Click(object sender, System.EventArgs e)

{

string strADRoot="";

string strName="";

if(comboBox1.Text==""||comboBox2.Text=="")

{

strErr="请选择合适的OU";

goto e1;

}

if(checkBox1.Checked)

{

if(textBox1.Text=="")

{

strErr="请输入新公司名称";

goto e1;

}

}

if(checkBox2.Checked)

{

if(textBox2.Text=="")

{

strErr="请输入部门名称!";

goto e1;

}

}

if(checkBox3.Checked)

{

if(textBox4.Text=="请输入新领导的姓名全拼(域账号)")

{

strErr="请输入新领导的姓名全拼(域账号)!";

goto e1;

}

// string strNewManager="";

//搜索

//检查是否有重复的账号

DirectoryEntry su=new DirectoryEntry("LDAP://DC=test,DC=net");

DirectorySearcher searcher = new DirectorySearcher();

searcher.SearchRoot=su;

searcher.PropertiesToLoad.Add("CN");

searcher.Filter = "(&(objectClass=user)(sAMAccountName="+textBox4.Text.ToString()+"))";

searcher.SearchScope = SearchScope.Subtree;

searcher.Sort = new SortOption("sAMAccountName", SortDirection.Ascending);

SearchResultCollection results = searcher.FindAll();

DialogResult dlResult;

string strMess="您确认要将该OU下的用户的领导更改为:"+results[0].Properties["CN"][0].ToString();

dlResult=MessageBox.Show(this,strMess,"请确认",MessageBoxButtons.YesNo,

MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,

MessageBoxOptions.RightAlign);

if(dlResult == DialogResult.Yes)

{

strManager= results[0].Properties["adspath"][0].ToString();

strManager=strManager.Remove(0,7);

}

else

{

textBox4.Text="请重新输入新领导的姓名全拼(域账号)!";

strErr="请重新输入新领导的姓名全拼(域账号)!";

goto e1;

}

}

if((!checkBox3.Checked)&&(!checkBox2.Checked)&&(!checkBox1.Checked))

{

strErr="请选择要修改的项目!";

goto e1;

}

strADRoot="LDAP://"+str+",DC=test,DC=net";

DirectoryEntry de=new DirectoryEntry();

de.Path=strADRoot;

textBox3.Text="";

try

{

foreach(DirectoryEntry chm in de.Children)

{

string strType="";

strType=chm.SchemaClassName.ToString();

if(strType.ToUpper()=="USER")

{

strName=chm.Name.ToString();

//如果选中了修改领导的话

if(checkBox3.Checked)

{

if(chm.Properties.Contains("manager"))

{

chm.Properties["manager"][0]=strManager;

chm.CommitChanges();

textBox3.Text=textBox3.Text+chm.Name .ToString()+"的领导修改成功!/r/n";

}

else

{

chm.Properties["manager"].Add(strManager);

chm.CommitChanges();

textBox3.Text=textBox3.Text+chm.Name .ToString()+"的领导添加成功!/r/n";

}

}

//修改公司

if(checkBox1.Checked)

{

if(chm.Properties.Contains("company"))

{

chm.Properties["company"][0]=textBox1.Text.ToString();

chm.CommitChanges();

textBox3.Text=textBox3.Text+chm.Name .ToString()+"的公司名称修改成功!/r/n";

}

else

{

chm.Properties["company"].Add(textBox1.Text.ToString());

chm.CommitChanges();

textBox3.Text=textBox3.Text+chm.Name .ToString()+"的公司名称添加成功!/r/n";

//textBox3.Text=textBox3.Text+ch1.Name .ToString()+"/r/n";

}

}

//修改部门

if(checkBox2.Checked)

{

if(chm.Properties.Contains("department"))

{

chm.Properties["department"][0]=textBox2.Text.ToString();

chm.CommitChanges();

textBox3.Text=textBox3.Text+chm.Name .ToString()+"的部门名称修改成功!/r/n";

}

else

{

chm.Properties["department"].Add(textBox2.Text.ToString());

chm.CommitChanges();

//textBox3.Text=textBox3.Text+ch1.Name .ToString()+"/r/n";

textBox3.Text=textBox3.Text+chm.Name .ToString()+"的部门名称添加成功!/r/n";

}

}

}

}

MessageBox.Show("修改成功!");

}

catch(Exception ex)

{

strErr=ex.Message;

goto e1;

}

e1: if(strErr!="")

{

MessageBox.Show(strErr);

}

}

private void comboBox2_SelectedIndexChanged(object sender, System.EventArgs e)

{

//str=listBox1.SelectedItem.ToString();

str=comboBox2.SelectedItem.ToString()+","+comboBox1.SelectedItem.ToString();

DirectoryEntry de1=new DirectoryEntry();

de1.Path="LDAP://"+str+",DC=test,DC=net";

try

{

comboBox3.Items.Clear();

comboBox3.Text="";

comboBox3.Refresh();

foreach (DirectoryEntry ch1 in de1.Children)

{

// textBox1.Text=textBox1.Text+str+"/r/n";//ch.Properties["adpath"][0].ToString();

string str1="";

str1=ch1.SchemaClassName.ToString();

if (str1=="organizationalUnit")

{

comboBox3.Items.Add(ch1.Name.ToString());

}

}

de1.Close();

//textBox1.Text=textBox1.Text+"--------------next------------------------/r/n";

// MessageBox.Show("finish!!!");

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

private void comboBox3_SelectedIndexChanged(object sender, System.EventArgs e)

{

str=comboBox3.SelectedItem.ToString()+","+comboBox2.SelectedItem.ToString()+","+comboBox1.SelectedItem.ToString();

DirectoryEntry de1=new DirectoryEntry();

de1.Path="LDAP://"+str+",DC=test,DC=net";

try

{

comboBox4.Items.Clear();

comboBox4.Text="";

comboBox4.Refresh();

foreach (DirectoryEntry ch1 in de1.Children)

{

// textBox1.Text=textBox1.Text+str+"/r/n";//ch.Properties["adpath"][0].ToString();

string str1="";

str1=ch1.SchemaClassName.ToString();

if (str1=="organizationalUnit")

{

comboBox4.Items.Add(ch1.Name.ToString());

}

}

de1.Close();

//textBox1.Text=textBox1.Text+"--------------next------------------------/r/n";

// MessageBox.Show("finish!!!");

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

private void comboBox4_SelectedIndexChanged(object sender, System.EventArgs e)

{

str=comboBox4.SelectedItem.ToString()+","+comboBox3.SelectedItem.ToString()+","+comboBox2.SelectedItem.ToString()+","+comboBox1.SelectedItem.ToString();

DirectoryEntry de1=new DirectoryEntry();

de1.Path="LDAP://"+str+",DC=test,DC=net";

try

{

comboBox6.Items.Clear();

comboBox6.Text="";

comboBox6.Refresh();

foreach (DirectoryEntry ch1 in de1.Children)

{

// textBox1.Text=textBox1.Text+str+"/r/n";//ch.Properties["adpath"][0].ToString();

string str1="";

str1=ch1.SchemaClassName.ToString();

if (str1=="organizationalUnit")

{

comboBox6.Items.Add(ch1.Name.ToString());

}

}

de1.Close();

//textBox1.Text=textBox1.Text+"--------------next------------------------/r/n";

// MessageBox.Show("finish!!!");

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

private void comboBox6_SelectedIndexChanged(object sender, System.EventArgs e)

{

str=comboBox6.SelectedItem.ToString()+","+comboBox4.SelectedItem.ToString()+","+

comboBox3.SelectedItem.ToString()+","+comboBox2.SelectedItem.ToString()+","+

comboBox1.SelectedItem.ToString();

DirectoryEntry de1=new DirectoryEntry();

de1.Path="LDAP://"+str+",DC=test,DC=net";

try

{

comboBox5.Items.Clear();

comboBox5.Text="";

comboBox5.Refresh();

foreach (DirectoryEntry ch1 in de1.Children)

{

// textBox1.Text=textBox1.Text+str+"/r/n";//ch.Properties["adpath"][0].ToString();

string str1="";

str1=ch1.SchemaClassName.ToString();

if (str1=="organizationalUnit")

{

comboBox5.Items.Add(ch1.Name.ToString());

}

}

de1.Close();

//textBox1.Text=textBox1.Text+"--------------next------------------------/r/n";

// MessageBox.Show("finish!!!");

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

private void comboBox5_SelectedIndexChanged(object sender, System.EventArgs e)

{

str=comboBox5.SelectedItem.ToString()+","+comboBox6.SelectedItem.ToString()+","+comboBox4.SelectedItem.ToString()+","+

comboBox3.SelectedItem.ToString()+","+comboBox2.SelectedItem.ToString()+","+

comboBox1.SelectedItem.ToString();

}

}

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多