首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.DirectoryServices很慢吗?

System.DirectoryServices很慢吗?
EN

Stack Overflow用户
提问于 2009-12-04 19:37:51
回答 2查看 8.6K关注 0票数 4

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

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

VPN上的网络连接没有问题,只是这段代码运行得很慢。

代码语言:javascript
复制
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);
                }
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-12-04 20:22:11

另一个建议是直接使用System.DirectoryServices.Protocols;您的代码将如下所示:

代码语言:javascript
复制
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]);
    }
}
票数 5
EN

Stack Overflow用户

发布于 2009-12-04 20:06:02

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1846436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档