繁体   English   中英

System.DirectoryServices很慢

[英]System.DirectoryServices is slow

我正在使用我的公司AD对用户进行身份验证。 此代码正在运行,但需要超过25-30秒才能返回DirectorySearcher结果。 我该怎么做才能改善响应时间?

public bool ADauthentication(string userName,string password)
        {
            try
            {
                string domain = ConfigurationManager.AppSettings["DirectoryDomain"];
                string path = ConfigurationManager.AppSettings["DirectoryPath"];
                string domainAndUserName = domain + @"\" + userName;
                DirectoryEntry entry = new DirectoryEntry(path+"CN=Users,DC=myDomain,DC=com", userName, password);
                entry.AuthenticationType = AuthenticationTypes.Secure;
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(SAMAccountName=" + userName+")";
                search.PropertiesToLoad.Add("CN");
                SearchResult result = search.FindOne();
                if (result == null)
                {
                    return false;
                }
                return true;
            }
            catch(Exception ex)
            {
                log.Error($"Error: {ex.ToString()}");
                return false;
            }
        }

我遇到了与AD类似的问题,但我通过缓存结果解决了这个问题。 您可以创建一些后台进程来同步AD和您的数据源。

请改用System.DirectoryServices.AccountManagement命名空间:

https://msdn.microsoft.com/en-us/library/bb154889(v=vs.110).aspx

using (var context = new PrincipalContext(ContextType.Domain))
{
    return context.ValidateCredentials(username, password);
}

暂无
暂无

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

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