2013-08-17 44 views
2

我想查詢域名,以確定:無法提取信息

  1. 用戶是有效用戶(以及是否有正確的密碼)
  2. 用戶啓用
  3. 用戶所屬的組x

我的開發機器不屬於這個域名。 我想通過我的應用程序指定用戶名和密碼

我使用System.DirectoryServices.AccountManagement命名空間,因爲這似乎是這樣做的最有效的方式,但是我一直在努力獲得最基本的的信息從我的域控制器中刪除。

我可以通過其他工具探索LDAP。

首先測試是收集用戶信息,下面的代碼在用戶返回null。 然而用戶是有效的。

我在做什麼錯?

// set up domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "server","CN=Users,DC=doom,DC=home", "ldapuser","password"); 

    // get user contect 
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.Name, username); 

//is user locked? 
var locked = user.Enabled; 

更新:

在定義了綁定方法如下,我現在收到錯誤 「關於域的信息不能被檢索(1355)。」

var ctx = new PrincipalContext(ContextType.Domain, "server", "DC=doom,DC=home", ContextOptions.SimpleBind, "ldapuser", "password"); 
+0

此博客應該爲您設置正確的方向http://stackoverflow.com/questions/3929561/how-to-get-active-directory-attributes-not-represented-by-the-userprincipal-clas – qamar

回答

7

Sorted。

此答案解決了我在嘗試連接到不屬於我的域控制器時遇到的兩個問題。

這篇文章讓我最後的答案: http://elegantcode.com/2009/03/21/one-scenario-where-the-systemdirectoryservices-accountmanagement-api-falls-down/

  1. 你需要定義在上下文中的綁定(即ContextOptions.SimpleBind)
  2. 必須設置域名服務器在您的網絡適配器DNS設置作爲第一個使用的DNS服務器。

我現在可以連接到我的AD並收集數據。

+0

完美工作爲了我!用於定義綁定的完整語法是var context = new PrincipalContext(ContextType.Domain,「DOMAIN_NAME」,「DC = companyname,DC = com」,ContextOptions.SimpleBind,「User1」,「password123」);. – John81