我在表单中使用以下代码,但是我收到错误
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”名称在当前上下文中不存在
答案 0 :(得分:2)
你是否在.cs文件的顶部有using System.DirectoryServices.AccountManagement;
指令,其中存在这种Passwordexpire方法?
答案 1 :(得分:0)
添加对System.DirectoryServices
.Net DLL的引用。
然后是zaitsman的回答:
using System.DirectoryServices.AccountManagement;