Guest User

Untitled

a guest
Aug 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. Exception while accessing AD in Service Layer
  2. private SearchResult GetUserById(string id)
  3. {
  4. SearchResult result = null;
  5. string targetEnviroment = this.serviceApplication.TargetEnvironment;
  6. try
  7. {
  8. DirectorySearcher ldapSearch = new DirectorySearcher();
  9. ldapSearch.SearchRoot = this.GetDirectoryEntry();
  10. if (targetEnviroment == "XYZ")
  11. {
  12. ldapSearch.Filter = "(&(objectClass=person)(userProperty=" + id + "))";
  13. }
  14. else
  15. {
  16. ldapSearch.Filter = "(&(objectClass=user)(samAccountName=" + id + "))";
  17. }
  18. ldapSearch.SearchScope = SearchScope.Subtree;
  19. result = ldapSearch.FindOne();
  20. }
  21. catch (Exception ex)
  22. {
  23. throw;
  24. }
  25. return result;
  26. }
  27.  
  28. public CorpDirUser CorpDir_GetUserById(string userId)
  29. {
  30. CorpDirUser result = null;
  31. string targetEnviroment = this.serviceApplication.TargetEnvironment;
  32. try
  33. {
  34. DirectoryEntry entry = this.GetUserById(userId).GetDirectoryEntry();
  35. if (entry != null)
  36. {
  37. if (targetEnviroment == "XYZ")
  38. {
  39. result = MapToUserFromXYZLDAP(entry);
  40. }
  41. else
  42. {
  43. result = MapToUserFromZYXLDAP(entry);
  44. }
  45. }
  46. }
  47. catch (Exception ex)
  48. {
  49. // logging
  50. }
  51. return result;
  52. }
  53.  
  54. private DirectoryEntry GetDirectoryEntry()
  55. {
  56. string pathath = this.serviceApplication.CorpDirLDAPPath;
  57. string account = this.serviceApplication.CorpDirServiceAccountName;
  58. string password = this.serviceApplication.CorpDirServiceAccountPassword;
  59. return new DirectoryEntry("LDAP://" + path, account, password, AuthenticationTypes.None);
  60. }
Add Comment
Please, Sign In to add comment