C# AD 用户密码过期

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

错误是:

The type or namespace name User Principal could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name Principal Searcher could not be found (are you missing a using directive or an assembly reference?)
The name 'MatchType' does not exist in the current context

最佳答案

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

关于C# AD 用户密码过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15308851/

相关文章:

c# - RC4 加密 KSA key 越界

c# - Sbyte[] 与 byte[][] 使用方法

nhibernate - Visual Studio .Net 中显示的 dll 版本不反射(reflect)引用的 dll 文件/产品版本

wpf - 如何提高 WPF 应用程序的性能

c# - 是否可以在不引用类的情况下引用静态类中的方法?

c# - SortedDictionary.ValueCollection 的枚举器行为不同于其他枚举器

c# - 代码优先自动生成代理类导航和集合属性为空

javascript - ASPxClientGridView.ApplyFilter 执行其工作后触发的客户端事件

c# - 如何覆盖自动属性的 C# 中的设置

c# - 重构 LINQ 查询