【问题标题】:System.DirectoryServices is slow?System.DirectoryServices 很慢?
【发布时间】:2025-11-28 19:15:01
【问题描述】:

当用户登录网站时,我使用下面的代码在活动目录中查找信息。在本地域上运行非常快,但是通过 VPN 到远程受信任域上运行时非常慢(大约需要 7 或 8 秒)。从同一个机器运行 dsa.msc 到远程域几乎与在本地运行它一样快。

我正在使用属性过滤来检索尽可能少的数据量,那么在这种情况下 System.DirectoryServices 是否存在固有的缓慢问题,或者是否有人对如何提高性能有任何提示?

VPN 网络连接正常,只是这段代码运行缓慢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var LDAPConnection = new DirectoryEntry("LDAP://domain/dc=domain,dc=com", "username", "password"))
            {
                LDAPConnection.AuthenticationType = AuthenticationTypes.Secure;
                using (DirectorySearcher Searcher = new DirectorySearcher(LDAPConnection))
                {
                    Searcher.Filter = "(&(&(objectclass=user)(objectcategory=person))sAMAccountName=username)";
                    Searcher.PropertiesToLoad.Add("mail");

                    SearchResult result = Searcher.FindOne(); //this line takes ages!

                    string EmailAddress = result.Properties["mail"][0].ToString();
                    Console.WriteLine(EmailAddress);
                }
            }
        }
    }
}

【问题讨论】:

    标签: c# performance active-directory directoryservices


    【解决方案1】:

    另一个建议是直接使用System.DirectoryServices.Protocols;你的代码看起来像:

    string filter = "(&(&(objectclass=user)(objectcategory=person))" + 
                    "sAMAccountName=username)";
    NetworkCredential credentials = new NetworkCredential(...);
    LdapDirectoryIdentifier directoryIdentifier = 
       new LdapDirectoryIdentifier("server", 389, false, false);
    using (LdapConnection connection = 
       new LdapConnection(directoryIdentifier, credentials, AuthType.Basic))
    {
        connection.Timeout = new TimeSpan(0, 0, 30);
        connection.SessionOptions.ProtocolVersion = 3;
        SearchRequest search = 
            new SearchRequest(query, filter, SearchScope.Base, "mail");
        SearchResponse response = connection.SendRequest(search) as SearchResponse;
        foreach(SearchResultEntry entry in response.Entries)
        {
            Console.WriteLine(entry.Attributes["mail"][0]);
        }
    }
    

    【讨论】:

    • 事实证明,将连接指向特定服务器会产生巨大的影响,所以我接受你的回答是因为它给了我尝试的提示。干杯。
    • 也从使用 authenticationtypes.fastbind 中得到了提升。
    • 你能举例说明变量“查询”应该包含什么吗?您的答案中没有声明
    • 一个显示query的值的例子可以看这个问题*.com/questions/13567685/…
    【解决方案2】:

    我从未尝试过您描述的场景(通过 VPN 连接到 Active Directory),但您标记的行是导致连接打开的行。在调用 FindOne 之前,您没有连接到服务器。我的猜测是建立连接需要 7-8 秒。

    如果您在 * 上找不到确切答案,请试试这个论坛:http://directoryprogramming.net/forums/default.aspx(我并不是说 * 没有帮助,但我在 DirectoryProgramming.net 论坛上找到了我的广告/ldap 问题的一些答案)。

    【讨论】:

    • +1 获得不错的论坛建议;我也在那里解决了一些 LDAP 问题
    • 这个论坛改名问乔·卡普兰也没什么坏处;)