gpt4 book ai didi

c# - System.DirectoryServices 很慢?

转载 作者:太空狗 更新时间:2023-10-30 00:58:56 25 4
gpt4 key购买 nike

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

最佳答案

另一个建议是直接使用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]);
}
}

关于c# - System.DirectoryServices 很慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1846436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com