C#AD用户密码到期

时间:2013-03-09 08:41:00

标签: c# c#-4.0 c#-3.0

我在表单中使用以下代码,但是我收到错误

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”名称在当前上下文中不存在

2 个答案:

答案 0 :(得分:2)

你是否在.cs文件的顶部有using System.DirectoryServices.AccountManagement;指令,其中存在这种Passwordexpire方法?

答案 1 :(得分:0)

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

然后是zaitsman的回答:

using System.DirectoryServices.AccountManagement;