从Active Directory获取用户

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从Active Directory获取用户相关的知识,希望对你有一定的参考价值。

  1. string sAMAccountName = "<login name>";
  2. string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", sAMAccountName);
  3. string[] properties = new string[] { "fullname" };
  4. //you must provide a username and password to authenticate (this is not the username and password of the person you're looking for):
  5. DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + "<domain>", "<username>", "<password>");
  6. DirectorySearcher searcher = new DirectorySearcher(adRoot);
  7. searcher.SearchScope = SearchScope.Subtree;
  8. searcher.ReferralChasing = ReferralChasingOption.All;
  9. searcher.PropertiesToLoad.AddRange(properties);
  10. searcher.Filter = filter;
  11. SearchResult result = searcher.FindOne();
  12. DirectoryEntry de = result.GetDirectoryEntry();
  13.  
  14. //you can now get the properties of the directory entry this way:
  15. string property = de.Properties["<property name>"].Value.ToString();
  16.  
  17. /*some examples of property names:
  18. co (country)
  19. c (country abbreviation)
  20. countryCode
  21. mail
  22. givenName (first name)
  23. title
  24. mobile
  25. sAMAccountName
  26. sn (last name)
  27. st (state)
  28. street
  29. streetAddress
  30. telephoneNumber
  31. postalCode
  32. */

以上是关于从Active Directory获取用户的主要内容,如果未能解决你的问题,请参考以下文章

LDAP:如何从 Active Directory 中获取所有用户和组

给定 UserPrincipal 对象,如何从 Active Directory 中获取“公司”和“部门”?

如何检索 Active Directory 用户列表

我可以将 Azure Active Directory 身份验证连接到现有项目吗?

使用 LDAP 在 Active Directory 中查找用户 - 没有用户?

LDAP 获取 Active Directory 中用户的主要组名