Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpGet]
- public IHttpActionResult ListProperties(string domainName, string userName, string password)
- {
- try
- {
- using (DirectoryEntry dEntry = new DirectoryEntry("LDAP://" + domainName, userName, password))
- {
- DirectorySearcher dSearcher = new DirectorySearcher(dEntry)
- {
- Filter = "(&(objectClass=user)(mail=" + userName + "))"
- };
- SearchResult sResult = dSearcher.FindOne();
- Dictionary<string, string> resultDictionary = new Dictionary<string, string>
- {
- {"Name", GetProperty(sResult,"cn")},
- {"Email", GetProperty(sResult,"mail")}
- };
- return Ok(resultDictionary.ToList());
- }
- }
- catch (Exception ex)
- {
- return BadRequest(ex.Message);
- }
- }
- private string GetProperty(SearchResult searchResult, string propertyName)
- {
- if (searchResult.Properties.Contains(propertyName))
- {
- return searchResult.Properties[propertyName][0].ToString();
- }
- return string.Empty;
- }
- $(document).ready(function () {
- $.ajax({
- type: "GET",
- url: "../api/xxxxxxx/ListProperties",
- data: { domainName: "mydomain.xxx.xx", userName: "MyUsername", password: "MyPassword" },
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: function (data) { console.log(JSON.stringify(data)); },
- failure: function (data) { console.log(0); },
- error: function (data) { console.log(1); }
- });
- });
- System.Runtime.InteropServices.COMException HResult=0x8007203A Message=The server is not operational.
Add Comment
Please, Sign In to add comment