【问题标题】:AD - LDAP error timeout in search with DirectoryEntryAD - 使用 DirectoryEntry 搜索时出现 LDAP 错误超时
【发布时间】:2023-03-06 07:32:01
【问题描述】:

我正在使用这个连接到 AD:

public void ValidateCredentials(string username, string password, out ClaimsIdentity identity)
    {
        using (DirectoryEntry entry = new DirectoryEntry())
        {
            entry.RefreshCache();
            entry.Username = username;
            entry.Password = password;
            DirectorySearcher searcher = new DirectorySearcher(entry);
            searcher.ClientTimeout = TimeSpan.FromMinutes(2);
            searcher.ServerTimeLimit = TimeSpan.FromMinutes(2);
            searcher.Filter = "(&(&(objectclass=user)(objectcategory=person))" +
            "sAMAccountName=" + username + ")";
            SearchResult srResult = searcher.FindOne();

            identity = new ClaimsIdentity();
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, username));
        }
    }

这样,如果我输入的用户名和密码错误,它会给我一个错误的用户名或密码错误,如果我输入一个正确的用户名和密码,它允许我登录,但如果我输入一个存在的用户名并且密码错误,它给我一个超时错误(30秒):

由于超时时间已过,此操作返回。

尝试使用 ClientTimeout 和 ServerTimeLimit 增加超时时间,但没有任何反应。

也可以尝试这样做:

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

但是服务器给了我一个我不支持的错误。

我对想法持开放态度。

提前致谢。

问候

编辑:我补充说,如果它有任何用处,我们正在通过能够减慢速度的 VPN 来做到这一点。

【问题讨论】:

  • 不幸的是,我无法复制这个。我复制了你的代码,但是如果我给它一个正确的用户名和错误的密码,我会立即得到一个“用户名或密码不正确”的异常。
  • 你打电话给entry.RefreshCache()有什么原因吗?删除它可能无法解决您的问题,但这完全没有必要,只会减慢您的方法。 (告诉它获取它绑定到的对象的所有现有属性的值,可能是域的根节点)
  • 你好!感谢您的回答,我添加了它(entry.RefreshCache()),因为据我所知,它可以提高查询的性能,但是如果您说没有必要将其取出,非常感谢。

标签: c# active-directory ldap timeout directoryentry


【解决方案1】:

问题是他们给我的LDAP url没有优化好,他们给了我一个新的url,问题就解决了

【讨论】:

    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 2019-12-11
    • 2010-09-14
    • 2018-04-27
    • 2020-01-21
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多