2013-03-09 54 views
2

我正在使用表格下面的代碼,但我得到一個錯誤C#AD用戶密碼過期

public DataTable Passwordexpire() 
{ 
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

    UserPrincipal userTemplate = new UserPrincipal(ctx); 
    userTemplate.AdvancedSearchFilter.AccountExpirationDate(DateTime.Today.AddDays(3), MatchType.LessThanOrEquals); 

    PrincipalSearcher searcher = new PrincipalSearcher(userTemplate); 


    foreach (Principal foundPrincipal in searcher.FindAll()) 
    { 
    UserPrincipal foundUser = (foundPrincipal as UserPrincipal); 

    if (foundUser != null) 
    { 
     DataTable dt = new DataTable(); 
     dt.Columns.Add("AccountName"); 
     dt.Columns.Add("Name"); 
     dt.Columns.Add("Empolyee ID"); 
     dt.Columns.Add("Company"); 

     foreach (SearchResult sResultSet in Dsearch.FindAll()) 
     { 
     DataRow dr = dt.NewRow(); 
      dr[0] = (GetProperty(sResultSet, "samaccountname")); 
      dr[1] = (GetProperty(sResultSet, "name")); 
      dr[2] = (GetProperty(sResultSet, "ExtensionAttribute2")); 
      dr[3] = (GetProperty(sResultSet, "Company")); 
      dt.Rows.Add(dr); 
     } 
     return dt; 
    } 
} 

的錯誤是:

類型或命名空間名稱的用戶主體找不到( )
無法找到類型或名稱空間名稱Principal Searcher(您是否缺少使用指令或程序集引用?)
名稱'MatchType'在目前情況下不存在

回答

0

添加對System.DirectoryServices .Net DLL的引用。

然後zaitsman的回答是:

using System.DirectoryServices.AccountManagement; 
+0

Thx它的工作。名稱'Dsearch'在當前上下文中不存在。你可以糾正代碼 – user113784 2013-03-09 09:04:35

2

你有在cs文件中,該Passwordexpire方法存在的頂部using System.DirectoryServices.AccountManagement;指令?

+0

+1良好的體育精神,你剛纔打我,但它只是解決方案 – 2013-03-09 08:45:15

+0

THX一半的答覆,在形式上採用蔭和它不存在 – user113784 2013-03-09 08:48:47

+0

傑里米·湯普森,是的,我種錯過了參考一半,因爲它對我來說更加明顯,你不會訪問你沒有引用的東西,但我偶爾會發現自己在額外的類中輸入類型,我忘記提及命名空間 – zaitsman 2013-03-09 08:52:33