简体   繁体   中英

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?

Already a person asked this question in SO

Is using DirectoryServices.NativeObject slow/bad?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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