简体   繁体   English

Asp.net中的Active Directory性能问题

[英]Active Directory Performance issue in Asp.net

I have using active directory login 我使用了活动目录登录

I got some performance issue 我遇到了一些性能问题

This is my code 这是我的代码

 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;
        }

When i enter correct user name and password, it's work well and fast. 当我输入正确的用户名和密码时,它可以正常且快速地工作。 But when i enter wrong username and password ,then it's loading very slow. 但是,当我输入错误的用户名和密码时,加载速度很慢。


This two line coding 这两行编码

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

taking long time to return the result while login inputs are wrong. 登录输入错误时需要花费很长时间返回结果。


So My question is 所以我的问题是

  • Why those line taking long time to load, whenever i put wrong login details? 每当我输入错误的登录详细信息时,为什么这些行需要很长时间加载?
  • Why those line taking quick time to load, whenever i put correct logindetails? 为什么每当我输入正确的logindetails时,这些行就需要快速加载时间?

Already a person asked this question in SO 已经有人问过这个问题

Is using DirectoryServices.NativeObject slow/bad? 使用DirectoryServices.NativeObject速度慢/不好吗?

But no one answering for that. 但没人回答。 Please tell me the solution :) 请告诉我解决方法:)

It takes a long time because it has to check every object to compare with the input as it never find the correct one. 这需要很长时间,因为它必须检查每个对象以与输入进行比较,因为它永远找不到正确的对象。

However, this is a unnecessarily complicated way to authenticate users on active directory. 但是,这是在活动目录上对用户进行身份验证的不必要的复杂方法。

This is much easier: 这要容易得多:

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

Then update the values afterwards. 然后更新值。

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

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