LDAP 服务器不可用

本文关键字:服务器 LDAP | 更新日期: 2023-09-27 17:55:17

我完全是这个的新手

尝试使用 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 服务器设置是否正确:

    主机
  • :本地主机
  • 端口:389
  • 基本DN:dc=maxcrc,dc=com
  • 网址: ldap://localhost:389/dc=maxcrc,dc=com

使用 Softerra LDAP 浏览器进行测试

从头到尾的任何教程将不胜感激...

LDAP 服务器不可用

我一直面临同样的问题,我找到了解决方案。

我能够使用以下代码轻松连接:

 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 */

我也有类似的问题。事实证明,我必须在对象初始化中传递用户名和密码。请尝试使用如下语句:

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

还要确保您的用户名中包含域。

例如

userName = "mydomainname" + "''" + "john_jacobs"

使用以下构造函数重载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

在我的环境中,我必须仅使用域控制器主机名创建主体上下文,然后单独验证用户凭据。

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();
}

此示例允许对用户名的所有三个变体进行验证:

  • 测试用户
  • MyDomain''TestUser
  • TestUser@MyDomain.本地

您可能想尝试本地计算机地址:

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

如果这不起作用,我会启动 Wireshark,并在您尝试通过 Softerra 连接时让它捕获端口 389 上的流量。

在我使用 LDAP 和 .Net DirectoryServices 时,该错误通常意味着路径的语法或命名约定不正确,或者未指向有效的目录端点。

该错误可能是由于尝试以"匿名"身份连接而未明确指定它。默认情况下,所有连接都是可协商的。因此,如果您尝试类似的事情,您可以尝试以下方法:

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;