连接LDAP服务器

本文关键字:服务器 LDAP 连接 | 更新日期: 2023-09-27 18:08:55

我想实现一个搜索ldap服务器(姓名,电话号码等)的函数

这是我写的(服务器地址是假的,但真实的服务器地址有相同的模式)

DirectoryEntry de = new DirectoryEntry("LDAP://aet7ldap.phony.com")                 
DirectorySearcher ds = new DirectorySearcher(de);
var test = ds.FindAll();

我知道还有其他构造函数(字符串路径,字符串用户,字符串密码),但我不知道我的用户名或密码,我不确定我是否需要一个。所以请帮我弄清楚如何在没有这些参数的情况下做到这一点(如果可能的话)。

我也试着写一个过滤器,但那是另一回事,因为首先我需要得到我的连接正确。但是我可以假设我必须使用这些参数(或列名吗?)我一直在到处阅读(例如"gn"表示给定的名称等等)?

连接LDAP服务器

听起来像是您正在尝试使用Directory入口方法连接到活动目录以查找用户,您没有提到如果您要对找到的用户进行任何操作,那么我将只给您代码来为您查找用户。

using(DirectoryEntry de = new DirectoryEntry("LDAP://servername/DC=phony,DC=com"))
using(DirectorySearcher ds = new DirectorySearcher(de))
{
    ds.Filter="(&(objectClass=user)(sAMAccountName="username"))";
    //I don't know exactly what criteria you're using to find the user
    ds.Filter="(&(objectClass=user)(distinguishedname="")(givenname=""))"
    ds.SearchScope = SearchScope.Subtree;
    //performing the search and assigning the result to result
    SearchResult result = ds.FindOne();
    if (result != null)
    {
        using(DirectoryEntry user = result.GetDirectoryEntry())
        {
            //put code here to deal with the user as you see fit.
        }
        lblOutput.Text = "User " + userName + " was found.";
    }
}

过滤器是找到你正在寻找的用户的最重要的部分,&意味着and,所以在上面代码中我给出的第一个例子中,你正在寻找一个类用户AND(用户名username)的对象。这很容易算出来。这里有一个指向所有Active Directory属性列表的链接,您应该能够在那里找到您要查找的内容。http://www.selfadsi.org/user-attributes.htm

问候,托利党山