The LDAP server is unavailable

2019-06-15 18:38发布

问题:

I'm a total newbie to this

Trying to connect to an ldap server with PrincipalContext. I have tried all solutions on this site to no avail.

Things I've tried:

PrincipalContext insPrincipalContext = 
   new PrincipalContext(ContextType.Domain);

PrincipalContext insPrincipalContext = 
   new PrincipalContext(ContextType.Domain, "ldap://localhost:389/dc=maxcrc,dc=com");

PrincipalContext insPrincipalContext = 
   new PrincipalContext(ContextType.Domain, "maxcrc.com");

All give the same result:

LDAP server not available

Only ContextType.Machine works basically.

Not sure if my LDAP server is set up correctly:

  • Host: localhost
  • Port: 389
  • Base DN: dc=maxcrc,dc=com
  • URL: ldap://localhost:389/dc=maxcrc,dc=com

Testing with Softerra LDAP Browser

Any tutorials from start to finish will be much appreciated...

回答1:

I have been facing the same issue and I found a solution.

I'm able to connect easily using following code:

 ADUser_Id = "domainName\\username"; //make sure user name has domain name.
 Password = "xxxx";
var context = new PrincipalContext(ContextType.Domain,"server_address", ADUser_Id,Password);
/* server_address = "192.168.15.36"; //don't include ldap in url */


回答2:

I had similar issues. It turned out that I had to pass username and password in the object initialization. Please try using a statement like below:

PrincipalContext insPrincipalContext = 
new PrincipalContext(ContextType.Domain, 
"ldap://localhost:389/dc=maxcrc,dc=com",
userName,
password);

Also make sure that your username has domain in it.

For example,

userName = "mydomainname" + "\\" + "john_jacobs"


回答3:

Use the following constructor overload for PrincipalContext:

public PrincipalContext(
    ContextType contextType,
    string name,
    string container
)

And separate the server name from the LDAP string:

PrincipalContext insPrincipalContext = 
   new PrincipalContext(ContextType.Domain, "localhost:389", "dc=maxcrc,dc=com");

https://msdn.microsoft.com/en-us/library/bb348316%28v=vs.110%29.aspx



回答4:

You may want to try your local machine address instead :

ldap://127.0.0.1:389/dc=maxcrc,dc=com

If that doesn't work, I'd fire up Wireshark, and have it capture traffic on port 389 as you're attempting to connect via Softerra.

In my time working with LDAP and .Net DirectoryServices, that error usually means the syntax or naming convention of the path is incorrect, or does not point to a valid directory end point.



回答5:

That error might be due to trying to connect as "Anonymous" without specifying it explicitly. By default all connections are Negotiable. So if you try something like that you could try the following:

LdapDirectoryIdentifier ldap = new LdapDirectoryIdentifier("My Hostname or IP Address",10389); //10389 might be your non default port
LdapConnection connection = new LdapConnection(ldap);
connection.AuthType = AuthType.Anonymous;


回答6:

In my environment I had to create the principal context with just the domain controller host name, and then separately validate the user credentials.

string domainControllerName = "PDC";
string domainName = "MyDomain"; // leave out the .Local, this is just to use as the prefix for the username if the user left it off or didn't use the principal address notation
string username = "TestUser";
string password = "password";

using (var ldap = new PrincipalContext(ContextType.Domain, domainControllerName))
{
    var usernameToValidate = username;
    if (!usernameToValidate.Any(c => c == '@' || c == '\\'))
            usernameToValidate = $"{domainName}\\{username}";

    if (!ldap.ValidateCredentials(username, context.Password, ContextOptions.SimpleBind))
        throw new UnauthorizedException();
}

This example allows for all three of these variations to the username to validate:

  • TestUser
  • MyDomain\TestUser
  • TestUser@MyDomain.Local


标签: c# asp.net ldap