Not able to connect through LDAP

0

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.

c#
asp.net
active-directory
ldap
asp.net-web-api2
asked on Stack Overflow May 24, 2018 by user3141505

1 Answer

0

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:

  • 389: LDAP - reads/writes to a single domain - this is the default if you don't specify a port
  • 636: LDAP over SSL - same as 389, but encrypted
  • 3268: Global catalog - read-only to your AD forest (if you have more than one domain, otherwise it's no different than 389, except read-only)
  • 3269: Global catalog over SSL

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.

answered on Stack Overflow May 24, 2018 by Gabriel Luci

User contributions licensed under CC BY-SA 3.0