Guest User

Untitled

a guest
May 24th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. [HttpGet]
  2. public IHttpActionResult ListProperties(string domainName, string userName, string password)
  3. {
  4. try
  5. {
  6. using (DirectoryEntry dEntry = new DirectoryEntry("LDAP://" + domainName, userName, password))
  7. {
  8. DirectorySearcher dSearcher = new DirectorySearcher(dEntry)
  9. {
  10. Filter = "(&(objectClass=user)(mail=" + userName + "))"
  11. };
  12. SearchResult sResult = dSearcher.FindOne();
  13. Dictionary<string, string> resultDictionary = new Dictionary<string, string>
  14. {
  15. {"Name", GetProperty(sResult,"cn")},
  16. {"Email", GetProperty(sResult,"mail")}
  17. };
  18.  
  19. return Ok(resultDictionary.ToList());
  20. }
  21. }
  22. catch (Exception ex)
  23. {
  24. return BadRequest(ex.Message);
  25. }
  26. }
  27.  
  28.  
  29. private string GetProperty(SearchResult searchResult, string propertyName)
  30. {
  31. if (searchResult.Properties.Contains(propertyName))
  32. {
  33. return searchResult.Properties[propertyName][0].ToString();
  34. }
  35. return string.Empty;
  36. }
  37.  
  38. $(document).ready(function () {
  39.  
  40.  
  41. $.ajax({
  42. type: "GET",
  43. url: "../api/xxxxxxx/ListProperties",
  44. data: { domainName: "mydomain.xxx.xx", userName: "MyUsername", password: "MyPassword" },
  45. contentType: "application/json; charset=utf-8",
  46. dataType: "json",
  47. success: function (data) { console.log(JSON.stringify(data)); },
  48. failure: function (data) { console.log(0); },
  49. error: function (data) { console.log(1); }
  50. });
  51. });
  52.  
  53. System.Runtime.InteropServices.COMException HResult=0x8007203A Message=The server is not operational.
Add Comment
Please, Sign In to add comment