LDAP服务器不可用

时间:2013-01-23 12:54:42

标签: c# asp.net ldap

我是这个的新手

尝试使用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
  • 网址:ldap:// localhost:389 / dc = maxcrc,dc = com

使用Softerra LDAP浏览器进行测试

从开始到结束的任何教程都将非常感谢......

6 个答案:

答案 0 :(得分:7)

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

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

 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 :(得分:3)

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

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

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

例如,

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

答案 2 :(得分: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

答案 3 :(得分:0)

您可能需要尝试使用本地计算机地址:

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

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

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

答案 4 :(得分: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;

答案 5 :(得分:0)

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

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
  •   
相关问题