通过LDAP连接到活动目录

51

我想用C#连接我们本地的Active Directory。

我找到了这个好的文档

但我真的不知道如何通过LDAP连接。

可以有人解释一下如何使用所要求的参数吗?

示例代码:

  static DirectoryEntry createDirectoryEntry()  
  {  
     // create and return new LDAP connection with desired settings  

     DirectoryEntry ldapConnection     = new DirectoryEntry("rizzo.leeds-art.ac.uk");  
     ldapConnection.Path               = "LDAP://OU=staffusers,DC=leeds-art,DC=ac,DC=uk";  
     ldapConnection.AuthenticationType = AuthenticationTypes.Secure;  
     return ldapConnection;  
  }  
我只有我们Active Directory服务器的主机名和IP地址。那么DC=xxx,DC=xx等等是什么意思?

8
ou代表组织单位,dc代表域组件。 - paul
3个回答

97

DC是您的域。如果您想连接到域example.com,则您的dc为:DC=example,DC=com

实际上,您不需要知道域控制器的主机名或IP地址(可能有很多个)。

只需想象您正在连接到域本身。因此,要连接到域example.com,您可以简单地编写

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com");
你已经完成了。
你也可以指定一个用户和密码来连接:
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com", "username", "password");

同时请确保始终使用大写字母书写LDAP。在某些地方读到应该尝试使用大写字母书写它,然后我的问题得到了解决。

directoryEntry.Path 属性允许您深入了解您的域。因此,如果您想在特定的OU(组织单位)中搜索用户,可以在那里设置它。

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com");
directoryEntry.Path = "LDAP://OU=Specific Users,OU=All Users,OU=Users,DC=example,DC=com";

这将匹配以下AD层次结构:

  • com
    • example
      • Users
        • All Users
          • Specific Users

只需按照从最深到最高的顺序编写层次结构即可。

现在你可以做很多事情

例如,通过账户名搜索用户并获取用户的姓氏:

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com");
DirectorySearcher searcher = new DirectorySearcher(directoryEntry) {
    PageSize = int.MaxValue,
    Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=AnAccountName))"
};

searcher.PropertiesToLoad.Add("sn");

var result = searcher.FindOne();

if (result == null) {
    return; // Or whatever you need to do in this case
}

string surname;

if (result.Properties.Contains("sn")) {
    surname = result.Properties["sn"][0].ToString();
}

1
那是一个答案!感谢您的帮助。 - Waren Schild
我有IpAddress和FQN;哪个更快?另外,一个IpAddress中是否会引用多个域名? - Dinesh Kumar P
我遇到了各种异常,直到我将过滤器简化为:"(cn=roland)"。从一个工作系统中,可以逐渐使过滤器“更好”(=更复杂)。 - Roland
1
你如何连接到特定的域控制器? - David Klempfner
3
将"LDAP://example.com"替换为"LDAP://myDomainController.example.com",就可以连接到指定的域控制器。请注意不要改变原意,并使翻译更加通俗易懂。 - The-First-Tiger

4

ldapConnection是服务器地址:ldap.example.com。Ldap.Connection.Path是您想要使用的ADS中路径,以LDAP格式插入。

OU=Your_OU,OU=other_ou,dc=example,dc=com

您从AD的最深处开始工作,向根添加dc=X,直到包括顶级域。

现在我缺少一个参数来进行身份验证,这与用户名的路径相同。

CN=username,OU=users,DC=example,DC=com

LDAP简介


1
如果您的电子邮件地址是“myname@mydomain.com”,请尝试将createDirectoryEntry()更改为以下内容。
如果XYZ在mydomain目录中存在,则为可选参数。
static DirectoryEntry createDirectoryEntry()
{
    // create and return new LDAP connection with desired settings
    DirectoryEntry ldapConnection = new DirectoryEntry("myname.mydomain.com");
    ldapConnection.Path = "LDAP://OU=Users, OU=XYZ,DC=mydomain,DC=com";
    ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
    return ldapConnection;
}

这基本上会检查com -> mydomain -> XYZ -> Users -> abcd。 主要函数如下所示:
try
{
    username = "Firstname LastName"
    DirectoryEntry myLdapConnection = createDirectoryEntry();
    DirectorySearcher search = new DirectorySearcher(myLdapConnection);
    search.Filter = "(cn=" + username + ")";
    ....    

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