当用户登录网站时,我使用下面的代码在活动目录中查找信息。在本地域上运行非常快,但是通过 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);
}
}
}
}
}