3

我在表单中使用以下代码,但出现错误

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;
   }
}

错误是:

找不到类型或命名空间名称 User Principal(是否缺少 using 指令或程序集引用?)
找不到类型或命名空间名称 Principal Searcher(是否缺少 using 指令或程序集引用?)
名称当前上下文中不存在“MatchType”

4

2 回答 2

3

您是否using System.DirectoryServices.AccountManagement;在 .cs 文件的顶部有此 Passwordexpire 方法所在的指令?

于 2013-03-09T08:43:57.330 回答
1

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

然后zaitsman的回答:

using System.DirectoryServices.AccountManagement;
于 2013-03-09T08:44:43.180 回答