0

在我的应用程序中,我需要从 Active Directory 获取用户电子邮件。

我遇到了System.DirectoryServices用于访问 Active Directory 的名称空间。我不知道它是如何工作的。我有几个问题。

我可以简单地使用这个命名空间并通过适当的查询访问 AD 吗?是否有任何先决条件,例如访问 LDAP 等。

请让我知道这实际上是如何工作的

仅供参考,我使用 .net 4.0

4

1 回答 1

1

如果您使用的是 .NET 3.5 及更高版本,则应查看System.DirectoryServices.AccountManagement(S.DS.AM) 命名空间。在这里阅读所有相关信息:

基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
   // find a user
   UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

   if(user != null)
   {
      // do something here....      
   }

   // find the group in question
   GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");

   // if found....
   if (group != null)
   {
       // iterate over members
       foreach (Principal p in group.GetMembers())
       {
          Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
          // do whatever you need to do to those members
       }
    }
}

新的 S.DS.AM 使得在 AD 中与用户和组一起玩变得非常容易!

于 2013-05-15T10:26:20.953 回答