簡體   English   中英

System.DirectoryServices的使用和限制

[英]System.DirectoryServices use And limitation

在我的應用程序中,我需要從Active Directory中獲取用戶電子郵件。

我遇到了用於訪問Active Directory的System.DirectoryServices命名空間。 我不知道它是如何工作的。 我有幾個問題。

我可以簡單地使用此命名空間並通過適當的查詢訪問AD嗎? 是否有任何先決條件,例如訪問LDAP等。

請讓我知道這是如何工作的

僅供參考我使用.net 4.0

如果您使用的是.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中的用戶和組玩起來非常容易!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM