我想检查Active Directory中是否禁用了用户。我写了下面的代码,但它不起作用。
private DataTable IsUserExists(DataTable dt)
{
DataTable temp = new DataTable();
temp.Columns.Add("DisplayName", typeof(string));
temp.Columns.Add("DOE", typeof(DateTime));
try
{
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
foreach (DataRow row in dt.Rows)
{
UserPrincipal users = UserPrincipal.FindByIdentity(context, row["DisplayName"].ToString());
if (users != null && (((UserPrincipal)users).Enabled.Value == true))
{
temp.Rows.Add(row["DisplayName"].ToString(), row["DOE"].ToString());
}
}
return temp;
}
}
catch (Exception)
{
return null;
}
}