I'm working on WEB API project using ASP.NET technology. This Web API need to check the user from AD Active Directory, as domain authentication using LDEP://
[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;
}
so I call this method with ajax for test only
$(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); }
});
});
Unfortunately, I always receiving bad request or this below error
System.Runtime.InteropServices.COMException HResult=0x8007203A Message=The server is not operational.
Can you please provide me with a guide how to resolve this issue, as I have never work with security programming before.
The error means that it is not getting any response from the domain controller. It could be that there is no network connectivity to the domain controller.
To test network connectivity, you can try this in PowerShell, using what you have in your domainName
variable in place of "domain.com"
(New-Object Net.Sockets.TcpClient).Connect("domain.com", 389)
No output means it succeeded. If it fails, it will tell you with a big red error message.
If that does not work, try one of these others. AD LDAP can work on any one of 4 ports:
If one of the other ports work, you can specify it in your code, like this:
new DirectoryEntry("LDAP://" + domainName + ":636", userName, password)
For port 3268, you can also use "GC://" instead of specifying the port:
new DirectoryEntry("GC://" + domainName, userName, password)
If none of those ports work, then you need to work out your network connectivity to your domain before continuing.
User contributions licensed under CC BY-SA 3.0