2013-02-11 130 views
35

我想用C#连接到我们的本地Active Directory。我发现this good documentation通过LDAP连接到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等等是什么意思?

+4

ou =组织单位,dc =域组件 – paul 2013-02-11 13:58:46

回答

59

DC是您的域名。如果你想连接到域的example.com比你的直流是:DC =例如,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
    • 例如
      • 用户
        • 所有用户
          • 特定用户

只需层次写从最深到最高。

Now you can do plenty of things

例如,通过账户名搜索用户,并得到其姓:

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

这是一个答案!感谢您的帮助。 – 2013-02-11 15:32:57

+0

我有IpAddress和FQN;哪个更快? 1个IP地址是否还有超过1个域名参与其中? – 2015-07-06 12:19:59

+0

我遇到过各种异常情况,直到我将过滤器简化为:'“(cn = roland)”'。从工作系统中,可以使过滤器逐渐“更好”(=更复杂) – Roland 2016-01-22 17:33:31

2

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

OU = Your_OU,OU = other_ou,DC =例如,DC = COM

你开始在最深OU工作返回到AD的根部,然后添加DC = X为每个域节,直到你拥有了一切,包括顶级域名

现在我错过参数进行身份验证,这个工程的相同用户名

CN =用户名,OU =用户,DC =例如道路, DC = com

Introduction to LDAP

0

如果您的电子邮件地址是“[email protected]”,尝试改变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 - >用户 - > ABCD

主要功能看起来如下:

try 
{ 
    username = "Firstname LastName" 
    DirectoryEntry myLdapConnection = createDirectoryEntry(); 
    DirectorySearcher search = new DirectorySearcher(myLdapConnection); 
    search.Filter = "(cn=" + username + ")"; 
    ....