我想检查用户是否在 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;
}
}