2013-01-18 165 views
3

我想從Active Directory中做一些簡單的報告。經過討論等我發現,如果我使用.NET FW 3.5及更高版本,則適合使用PrincipalContext。我想了解這些新功能的原理和我能做些什麼(不像DirectoryEntry)。在Active Directory中查詢PrincipalContext

代碼框架

// create your domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, 
    "YOURDOMAIN", "OU=SomeOU,DC=YourCompany,DC=com"); 

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// which has a password that will expire in 3 days or less 
UserPrincipal userTemplate = new UserPrincipal(ctx); 
userTemplate.AdvancedSearchFilter.AccountExpirationDate(DateTime.Today.AddDays(3), MatchType.LessThanOrEquals); 

// instantiate searcher 
PrincipalSearcher searcher = new PrincipalSearcher(userTemplate); 

// enumerate matching users 
foreach (Principal foundPrincipal in searcher.FindAll()) 
{ 
    UserPrincipal foundUser = (foundPrincipal as UserPrincipal); 

    if (foundUser != null) 
    { 
     // do something with users found - e.g. send e-mail 
    } 
} 

有可能通過代碼彌補登錄添加此屬性LDAP?:

  • 是幹什麼用的LDAP(版本2或3)
  • 如何設置運行LDAP的端口
  • 如果我需要SSL連接,該如何工作? (不同端口,必須有特殊要求)

另外,我可以用AdvancedSearchFilter這個條件嗎?
(我發現只有AccountExpirationDateAccountLockoutDate

  • 用戶的密碼將在不久的將來
  • 用戶密碼已過期
  • 檢查,如果用戶的密碼過期
  • 用戶帳戶到期(帳戶到期,無密碼)
  • 過期用戶帳號(帳號,無密碼)
  • 用戶帳號未過期
+1

如果您還沒有 - 絕對看MSDN文章[在.NET Framework 3.5管理目錄安全主體(http://msdn.microsoft.com/en- us/magazine/cc135979.aspx),它很好地展示瞭如何充分利用'System.DirectoryServices.AccountManagement'中的新功能(並且我相信這會回答很多問題) –

+0

您好,感謝鏈接到文章,我讀過,除AD之外我還嘗試過測試代碼,但不幸的是,我仍處於幾乎相同的情況。例如。當我鎖定用戶帳戶時,「IsAccountLockedOut」屬性的狀態總是False,如果我設置了帳戶有效期限,AccountLockoutTime屬性從未設置等。另外,許多其他屬性在UserPrincipal類中根本不在用戶之下。我可能不理解爲什麼要使用PrincipalContext而不是DirectoryEntry的理念。 – czWolfHunter

+0

對不起,我的錯。錯誤將發生在椅子和鍵盤之間,我真的不明白原理。一旦我有了結果,我會發出正確的答案。 – czWolfHunter

回答

相關問題