LDAP服务器不可用。

15

我完全是个新手

尝试使用PrincipalContext连接ldap服务器。我已经尝试了该网站上的所有解决方案,但都无济于事。

我尝试过的事情:

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");

所有的都会得到相同的结果:

LDAP服务器不可用

只有ContextType.Machine基本上有效。

不确定我的LDAP服务器是否设置正确:

  • 主机:localhost
  • 端口:389
  • 基础DN:dc=maxcrc,dc=com
  • URL:ldap://localhost:389/dc=maxcrc,dc=com

使用Softerra LDAP Browser进行测试

任何从开始到结束的教程将不胜感激...


3
这可能听起来很蠢,但是尝试使用LDAP://而不是ldap://,因为我知道过去对于我来说它已经修复了连接问题。 - freefaller
仍然得到相同的结果,但谢谢。 - Anarchy101
您能否使用任何标准的AD工具连接到LDAP服务器? - mellamokb
如果您所说的标准AD工具是DirectoryEntry,那么是的。我已经尝试过:DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE"); - Anarchy101
6个回答

15
我一直面临着同样的问题,不过我找到了一个解决方案。 我可以通过以下代码轻松连接:
 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 */

1
指定服务器主机名对我有用。我能够使用ADUser_Id中不带域的用户。好文章。 - vapcguy

5

我遇到了类似的问题。结果发现在对象初始化时需要传递用户名和密码。请尝试使用以下格式的语句:

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

请确保您的用户名中包含域名。
例如:
userName = "mydomainname" + "\\" + "john_jacobs"

对我来说,像你建议的那样直接将凭据传递到对象初始化中是有效的。 - Bjørn Fridal

1
在我的环境中,我只能使用域控制器主机名创建主体上下文,然后单独验证用户凭据。
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();
}

这个例子允许验证用户名的三种变体:
  • TestUser
  • MyDomain\TestUser
  • TestUser@MyDomain.Local

1

使用以下构造函数重载来创建PrincipalContext对象:

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

将服务器名称与LDAP字符串分开:

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


0

你可能想尝试使用本地机器地址:

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

如果这不起作用,我会启动Wireshark,并在您尝试通过Softerra连接时捕获端口389上的流量。
在我使用LDAP和.Net DirectoryServices的时间里,这个错误通常意味着路径的语法或命名约定不正确,或者没有指向有效的目录终点。

0

这个错误可能是由于尝试连接时未明确指定为“匿名”而导致的。 默认情况下,所有连接都是可协商的。因此,如果您尝试类似的操作,可以尝试以下方法:

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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接