国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
AD相關(guān)信息

domainName 域服務(wù)器名稱 例如 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服務(wù)器名稱/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 域服務(wù)器名稱



  2. using System;
  3. using System.DirectoryServices;
  4. namespace SystemFrameworks.Helper
  5. {
  6. ///
  7. ///活動目錄輔助類。封裝一系列活動目錄操作相關(guān)的方法。
  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. ///用戶登錄驗證結(jié)果
  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. ///用戶屬性定義標(biāo)志
  59. ///
  60. public enum ADS_USER_FLAG_ENUM
  61. {
  62. ///
  63. ///登錄腳本標(biāo)志。如果通過 ADSI LDAP 進行讀或?qū)懖僮鲿r,該標(biāo)志失效。如果通過 ADSI WINNT,該標(biāo)志為只讀。
  64. ///
  65. ADS_UF_SCRIPT = 0X0001,
  66. ///
  67. ///用戶賬號禁用標(biāo)志
  68. ///
  69. ADS_UF_ACCOUNTDISABLE = 0X0002,
  70. ///
  71. ///主文件夾標(biāo)志
  72. ///
  73. ADS_UF_HOMEDIR_REQUIRED = 0X0008,
  74. ///
  75. ///過期標(biāo)志
  76. ///
  77. ADS_UF_LOCKOUT = 0X0010,
  78. ///
  79. ///用戶密碼不是必須的
  80. ///
  81. ADS_UF_PASSWD_NOTREQD = 0X0020,
  82. ///
  83. ///密碼不能更改標(biāo)志
  84. ///
  85. ADS_UF_PASSWD_CANT_CHANGE = 0X0040,
  86. ///
  87. ///使用可逆的加密保存密碼
  88. ///
  89. ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 0X0080,
  90. ///
  91. ///本地賬號標(biāo)志
  92. ///
  93. ADS_UF_TEMP_DUPLICATE_ACCOUNT = 0X0100,
  94. ///
  95. ///普通用戶的默認賬號類型
  96. ///
  97. ADS_UF_NORMAL_ACCOUNT = 0X0200,
  98. ///
  99. ///跨域的信任賬號標(biāo)志
  100. ///
  101. ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 0X0800,
  102. ///
  103. ///工作站信任賬號標(biāo)志
  104. ///
  105. ADS_UF_WORKSTATION_TRUST_ACCOUNT = 0x1000,
  106. ///
  107. ///服務(wù)器信任賬號標(biāo)志
  108. ///
  109. ADS_UF_SERVER_TRUST_ACCOUNT = 0X2000,
  110. ///
  111. ///密碼永不過期標(biāo)志
  112. ///
  113. ADS_UF_DONT_EXPIRE_PASSWD = 0X10000,
  114. ///
  115. /// MNS 賬號標(biāo)志
  116. ///
  117. ADS_UF_MNS_LOGON_ACCOUNT = 0X20000,
  118. ///
  119. ///交互式登錄必須使用智能卡
  120. ///
  121. ADS_UF_SMARTCARD_REQUIRED = 0X40000,
  122. ///
  123. ///當(dāng)設(shè)置該標(biāo)志時,服務(wù)賬號(用戶或計算機賬號)將通過 Kerberos 委托信任
  124. ///
  125. ADS_UF_TRUSTED_FOR_DELEGATION = 0X80000,
  126. ///
  127. ///當(dāng)設(shè)置該標(biāo)志時,即使服務(wù)賬號是通過 Kerberos 委托信任的,敏感賬號不能被委托
  128. ///
  129. ADS_UF_NOT_DELEGATED = 0X100000,
  130. ///
  131. ///此賬號需要 DES 加密類型
  132. ///
  133. ADS_UF_USE_DES_KEY_ONLY = 0X200000,
  134. ///
  135. ///不要進行 Kerberos 預(yù)身份驗證
  136. ///
  137. ADS_UF_DONT_REQUIRE_PREAUTH = 0X4000000,
  138. ///
  139. ///用戶密碼過期標(biāo)志
  140. ///
  141. ADS_UF_PASSWORD_EXPIRED = 0X800000,
  142. ///
  143. ///用戶賬號可委托標(biāo)志
  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. ///根據(jù)指定用戶名和密碼獲得相應(yīng)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創(chuàng)建的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. ///根據(jù)用戶公共名稱取得用戶的 對象
  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. ///根據(jù)用戶公共名稱和密碼取得用戶的 對象。
  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. ///根據(jù)用戶賬號稱取得用戶的 對象
  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. ///根據(jù)用戶賬號和密碼取得用戶的 對象
  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. ///根據(jù)組名取得用戶組的 對象
  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. ///獲得指定 指定屬性名對應(yīng)的值
  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. ///獲得指定搜索結(jié)果 中指定屬性名對應(yīng)的值
  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. ///設(shè)置指定 的屬性值
  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. ///創(chuàng)建新的用戶
  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. ///創(chuàng)建新的用戶。默認創(chuàng)建在 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. ///如果用戶賬號已經(jīng)激活,返回 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. // 必須在判斷用戶密碼正確前,對賬號激活屬性進行判斷;否則將出現(xiàn)異常。
  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. // 必須在判斷用戶密碼正確前,對賬號激活屬性進行判斷;否則將出現(xiàn)異常。
  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. ///設(shè)置用戶密碼,管理員可以通過它來修改指定用戶的密碼。
  503. ///
  504. ///
  505. 用戶公共名稱
  506. ///
  507. 用戶新密碼
  508. public static void SetPassword(string commonName, string newPassword)
  509. {
  510. DirectoryEntry de = GetDirectoryEntry(commonName);
  511. // 模擬超級管理員,以達到有權(quán)限修改用戶密碼
  512. impersonate.BeginImpersonate();
  513. de.Invoke("SetPassword", new object[]{newPassword});
  514. impersonate.StopImpersonate();
  515. de.Close();
  516. }
  517. ///
  518. ///設(shè)置賬號密碼,管理員可以通過它來修改指定賬號的密碼。
  519. ///
  520. ///
  521. 用戶賬號
  522. ///
  523. 用戶新密碼
  524. public static void SetPasswordByAccount(string sAMAccountName, string newPassword)
  525. {
  526. DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName);
  527. // 模擬超級管理員,以達到有權(quán)限修改用戶密碼
  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. ///用戶模擬角色類。實現(xiàn)在程序段內(nèi)進行用戶角色模擬。
  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. ///構(gòu)造函數(shù)
  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. ///析構(gòu)函數(shù)
  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. 簡單的應(yīng)用
  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("該服務(wù)器不可操作"))
  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>

/// 必需的設(shè)計器變量。

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Windows 窗體設(shè)計器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼

//

}

/// <summary>

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

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows 窗體設(shè)計器生成的代碼

/// <summary>

/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改

/// 此方法的內(nèi)容。

/// </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 = "修改部門領(lǐng)導(dǎo)";

//

// 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 = "新的部門領(lǐng)導(dǎo)";

//

// 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 = "請輸入新領(lǐng)導(dǎo)的姓名全拼(域賬號)";

//

// 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>

/// 應(yīng)用程序的主入口點。

/// </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=="請輸入新領(lǐng)導(dǎo)的姓名全拼(域賬號)")

{

strErr="請輸入新領(lǐng)導(dǎo)的姓名全拼(域賬號)!";

goto e1;

}

// string strNewManager="";

//搜索

//檢查是否有重復(fù)的賬號

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下的用戶的領(lǐng)導(dǎo)更改為:"+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="請重新輸入新領(lǐng)導(dǎo)的姓名全拼(域賬號)!";

strErr="請重新輸入新領(lǐng)導(dǎo)的姓名全拼(域賬號)!";

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();

//如果選中了修改領(lǐng)導(dǎo)的話

if(checkBox3.Checked)

{

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

{

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

chm.CommitChanges();

textBox3.Text=textBox3.Text+chm.Name .ToString()+"的領(lǐng)導(dǎo)修改成功!/r/n";

}

else

{

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

chm.CommitChanges();

textBox3.Text=textBox3.Text+chm.Name .ToString()+"的領(lǐng)導(dǎo)添加成功!/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();

}

}

}

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
動態(tài)生成控件并取值
C#窗體——100以內(nèi)加法做題程序
.NET開發(fā)中的一些小技巧 - 團團的園子 - 博客園
C#入門代碼 - C Programmer's Cookbook - C 博客
c# windows開源google搜索引擎
C# System.DirectoryServices.DirectoryEntry 域用戶驗證
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服