繁体   English   中英

Asp.net中的Active Directory性能问题

[英]Active Directory Performance issue in Asp.net

我使用了活动目录登录

我遇到了一些性能问题

这是我的代码

 public bool IsAuthenticated(String domain, String username, String pwd)
        {

            String domainAndUsername = domain + @"\" + username;
            DirectoryEntry entry = new DirectoryEntry("LDAP://" + _path, domainAndUsername, pwd);

            try
            {   //Bind to the native AdsObject to force authentication.         
                Object obj = entry.NativeObject;

                DirectorySearcher search = new DirectorySearcher(entry);

                search.Filter = "(SAMAccountName=" + username + ")";
                search.PropertiesToLoad.Add("cn");
                SearchResult result = search.FindOne();

                if (null == result)
                {
                    return false;
                }

                //Update the new path to the user in the directory.
                _path = result.Path;
                _filterAttribute = (String)result.Properties["cn"][0];
            }
            catch (Exception ex)
            {
                throw new Exception("Error authenticating user. " + ex.Message);
            }

            return true;
        }

当我输入正确的用户名和密码时,它可以正常且快速地工作。 但是,当我输入错误的用户名和密码时,加载速度很慢。


这两行编码

Object obj = entry.NativeObject;
 DirectorySearcher search = new DirectorySearcher(entry);

登录输入错误时需要花费很长时间返回结果。


所以我的问题是

  • 每当我输入错误的登录详细信息时,为什么这些行需要很长时间加载?
  • 为什么每当我输入正确的logindetails时,这些行就需要快速加载时间?

已经有人问过这个问题

使用DirectoryServices.NativeObject速度慢/不好吗?

但没人回答。 请告诉我解决方法:)

这需要很长时间,因为它必须检查每个对象以与输入进行比较,因为它永远找不到正确的对象。

但是,这是在活动目录上对用户进行身份验证的不必要的复杂方法。

这要容易得多:

    public bool ValidateCredentials(string domain, string username, string password)
    {
        using (PrincipalContext context = new PrincipalContext(ContextType.Domain, domain))
        {
            return context.ValidateCredentials(username, password);
        }
    }

然后更新值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM