首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.DirectoryServices速度很慢

System.DirectoryServices速度很慢
EN

Stack Overflow用户
提问于 2017-08-17 23:49:48
回答 2查看 516关注 0票数 4

我正在努力验证用户使用我的公司广告。此代码正在运行,但返回DirectorySearcher结果的时间超过25-30秒。我能做些什么来改善它的响应时间?

代码语言:javascript
复制
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;
            }
        }
EN

回答 2

Stack Overflow用户

发布于 2017-08-18 00:05:42

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

票数 0
EN

Stack Overflow用户

发布于 2017-08-18 15:34:14

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

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

代码语言:javascript
复制
using (var context = new PrincipalContext(ContextType.Domain))
{
    return context.ValidateCredentials(username, password);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45739733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档