Quick way to retrieve user information Active Directory

前端 未结 5 863
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 04:33

How to query user information from Active Directory? I have code that works, but it\'s really slow. I\'m using C#. This is the code I currently use:

    sta         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 05:00

    The reason why your code is slow is that your LDAP query retrieves every single user object in your domain even though you're only interested in one user with a common name of "Adit":

    dSearcher.Filter = "(&(objectClass=user))";
    

    So to optimize, you need to narrow your LDAP query to just the user you are interested in. Try something like:

    dSearcher.Filter = "(&(objectClass=user)(cn=Adit))";
    

    In addition, don't forget to dispose these objects when done:

    • DirectoryEntry dEntry
    • DirectorySearcher dSearcher

提交回复
热议问题