我可以通过以下代码查看用户是否处于活动状态。
public class ADService
{
public bool? FindAccountStatusByEmail(string email)
{
using (DirectorySearcher dSearch = new DirectorySearcher())
{
dSearch.Filter = "(&(objectCategory=person)(sAMAccountName=*)(mail=" + email.Trim() + "))";
SearchResult sResult = dSearch.FindOne();
if (sResult != null)
{
DirectoryEntry de = sResult.GetDirectoryEntry();
return IsActive(de);
}
else
{
return null;
}
}
}
private bool IsActive(DirectoryEntry de)
{
if (de.NativeGuid == null) return false;
int flags = (int)de.Properties["userAccountControl"].Value;
return !Convert.ToBoolean(flags & 0x0002);
}
}
但是我需要使用C#生成Active Directory中上个月/给定日期范围内的已停用用户列表。
你能帮我吗?答案 0 :(得分:0)
最后,在这里。我开发了它。我的目标是:我需要找到自上次'x'天后停用的停用用户。
whenChanged
- 有时能够根据Active Directory的更改时间搜索Active Directory中的对象。
我们可以像在LDAP查询中一样使用此属性,唯一要注意的是日期/时间值的语法。该属性的语法如下:
YYYYMMDDHHmmss.sZ
(最后的首都Z是强制性的,表示祖鲁时间,与GMT相同。)
当我们要查询Active Directory以查找已禁用的用户类对象时,保存此信息的属性是userAccountControl
属性。如果帐户被禁用,userAccountControl:1.2.840.113556.1.4.803
将设置位2。查看更多userAccountControl
最后代码是:
public StringBuilder FindDisabledAccount(int sinceDay)
{
StringBuilder sb = new StringBuilder();
DateTime fromDate = DateTime.Today.AddDays(-sinceDay);
try
{
using (DirectorySearcher dSearch = new DirectorySearcher())
{
dSearch.PageSize = 100;
dSearch.CacheResults = false;
string fromDateStr = fromDate.Year.ToString() + fromDate.Month.ToString("D2") + fromDate.Day.ToString("D2") + "000000.0Z";
string toDateStr = DateTime.Today.Year.ToString() + DateTime.Today.Month.ToString("D2") + (DateTime.Today.Day + 1).ToString("D2") + "000000.0Z";
dSearch.Filter = "(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=2)"
+ "(whenChanged>=" + fromDateStr + ")(whenChanged<=" + toDateStr + "))";
dSearch.PropertiesToLoad.Add("sAMAccountName");
dSearch.PropertiesToLoad.Add("DisplayName");
dSearch.PropertiesToLoad.Add("mail");
dSearch.PropertiesToLoad.Add("distinguishedName");
dSearch.PropertiesToLoad.Add("whenChanged");
using (var sResult = dSearch.FindAll())
{
sb.AppendLine("EmpId,Name,Email,IsActive,LastChangedDate");
foreach (SearchResult result in sResult)
{
DirectoryEntry de = result.GetDirectoryEntry();
sb.AppendLine(de.Properties["sAMAccountName"].Value + "," + de.Properties["DisplayName"].Value + "," + de.Properties["mail"].Value + ",FALSE," + de.Properties["whenChanged"].Value);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return sb;
}