Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.DirectoryServices;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApplication7
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. string sAMAccountName = "10010";
  15. DirectoryEntry userRoot = GetDirectoryEntry("10.10.2.11", "test.com", "administrator", "P@ssw0rd");
  16. DirectorySearcher searcher = new DirectorySearcher(userRoot);
  17. //searcher.Filter = "(sAMAccountName=" + sAMAccountName + ")";
  18. searcher.Filter = "(&(objectClass=organizationalUnit)(name=" + "0102-01002.集团财务部" + "))";
  19. DirectoryEntry entry = searcher.FindOne().GetDirectoryEntry();
  20. string addresslistname = ModifyPathToIdentity(entry.Path);
  21. Console.WriteLine(addresslistname);
  22. Console.ReadKey();
  23. //string bb2 = adMgrExchange.EnableMail(addresslistname);
  24. }
  25. public static DirectoryEntry GetDirectoryEntry(string serverIp, string domain, string userName, string pwd)
  26. {
  27. DirectoryEntry de = new DirectoryEntry();
  28. de.Path = "LDAP://" + serverIp;
  29. de.Username = domain + "\\" + userName;
  30. de.Password = pwd;
  31. //eg:
  32. //de.Path = "LDAP://test.com/CN=Users;DC=Yourdomain";
  33. //de.Username = @"test\0202";
  34. //de.Password = "123456";
  35. return de;
  36. }
  37. public static string ModifyPathToIdentity(string path)
  38. {
  39. string identity = null;
  40. var arr = path.Split(',');
  41. for (int i = 0; i < arr.Length; i++)
  42. {
  43. var arr2 = arr[i].Split('=');
  44. arr[i] = arr2[1];
  45. }
  46. StringBuilder temp = new StringBuilder();
  47. string temp2 = arr[arr.Length - 2] + "." + arr[arr.Length - 1] + "/";
  48. temp.Append(temp2);
  49. for (int i = arr.Length - 3; i >= 0; i--)
  50. {
  51. temp.Append(arr[i] + "/");
  52. }
  53. string result = temp.ToString().Substring(0, temp.ToString().Length - 1);
  54.  
  55. return result;
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement