C# LDAP 服务器不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14480258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:58:34  来源:igfitidea点击:

The LDAP server is unavailable

c#asp.netldap

提问by Anarchy101

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.

尝试使用PrincipalContext. 我已经尝试了这个网站上的所有解决方案都无济于事。

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

LDAP 服务器不可用

Only ContextType.Machineworks basically.

只能ContextType.Machine基本工作。

Not sure if my LDAP server is set up correctly:

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

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

Testing with Softerra LDAP Browser

使用 Softerra LDAP 浏览器进行测试

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

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

回答by X3074861X

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.

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

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.

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

回答by objecto

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;

回答by Dhruv Rangunwala

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"

回答by Ravi Anand

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

回答by Owen Pauling

Use the following constructor overload for PrincipalContext:

使用以下构造函数重载PrincipalContext

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

And separate the server name from the LDAP string:

并将服务器名称与 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

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

回答by Chris Schaller

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:

此示例允许验证用户名的所有三种变体:

  • 测试用户
  • 我的域\测试用户
  • 测试用户@MyDomain.Local