15

我是这个的新手

尝试使用PrincipalContext. 我已经尝试了该站点上的所有解决方案,但均无济于事。

我尝试过的事情:

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 浏览器进行测试

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

4

6 回答 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 */
于 2015-12-28T13:18:00.940 回答
5

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

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

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

例如,

userName = "mydomainname" + "\\" + "john_jacobs"
于 2013-10-21T22:10:11.093 回答
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

于 2016-06-01T09:35:07.757 回答
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;
于 2013-10-11T09:14:02.500 回答
0

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

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

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

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

于 2013-01-24T23:26:49.747 回答
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.Local
于 2017-06-09T03:28:11.767 回答