我正在为特定 OU 中的用户搜索 Active Directory。我只获取在过去 30 天内登录的用户。
我的搜索过滤器查询是:
string query = "(&(objectCategory=person)(objectClass=user)((lastLogon<=" + new DateTime(DateTime.Now.AddDays(-30).Ticks) + ")(mail=*))";
我得到我用过的搜索过滤器无效:
string query = "(&(objectCategory=person)(objectClass=user)((lastLogon=*)(mail=*))";
没有错误
我将上次登录修改如下:
(lastLogon<=1)
我正在调用执行此操作的方法
public static DataTable GetADusers() {
try {
string ou = "OU";
using(PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Environment.UserDomainName, ou)) {
UserPrincipal user = new UserPrincipal(ctx);
using(PrincipalSearcher ps = new PrincipalSearcher(user)) {
DataTable results = new DataTable();
results.Columns.Add("DisplayName ");
results.Columns.Add("FirstName");
results.Columns.Add("Initial");
results.Columns.Add("LastName");
results.Columns.Add("mail");
results.Columns.Add("SamAccountName");
results.Columns.Add("DistinguishedName");
results.Columns.Add("lastLogon");
int count = 0;
int ctNull = 0;
foreach(Principal p in ps.FindAll()) {
UserPrincipal u = p as UserPrincipal;
if (u != null) {
DirectoryEntry entry = (DirectoryEntry) p.GetUnderlyingObject();
DirectorySearcher search = new DirectorySearcher(entry);
string query = "(&(objectCategory=person)(objectClass=user)((lastLogon<=" + new DateTime(DateTime.Now.AddDays( - 30).Ticks) + ")(mail=*))";
search.Filter = query;
search.PropertiesToLoad.Add("DisplayName");
search.PropertiesToLoad.Add("GivenName");
search.PropertiesToLoad.Add("Initials");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("SamAccountName");
search.PropertiesToLoad.Add("DistinguishedName");
search.PropertiesToLoad.Add("lastLogon");
SearchResultCollection mySearchResultColl = search.FindAll();
foreach(SearchResult sr in mySearchResultColl) {
DataRow dr = results.NewRow();
DirectoryEntry de = sr.GetDirectoryEntry();
dr["EmployeeID"] = de.Properties["EmployeeID"].Value;
dr["DisplayName "] = de.Properties["DisplayName"].Value;
dr["FirstName"] = de.Properties["GivenName"].Value;
dr["Initial"] = de.Properties["Initials"].Value;
dr["LastName"] = de.Properties["sn"].Value;
dr["mail"] = de.Properties["mail"].Value;
dr["SamAccountName"] = de.Properties["SamAccountName"].Value;
dr["DistinguishedName"] = de.Properties["DistinguishedName"].Value;
//prepare for last logon
if (de.Properties["lastLogon"] != null && de.Properties["lastLogon"].Count > 0) {
Int64 lastLogonThisServer = new Int64();
IADsLargeInteger lgInt = (IADsLargeInteger) de.Properties["lastLogon"].Value;
lastLogonThisServer = ((long) lgInt.HighPart << 32) + lgInt.LowPart;
dr["lastLogon"] = DateTime.FromFileTime(lastLogonThisServer).ToString();
}
else {
dr["lastLogon"] = DateTime.MinValue.ToString();
ctNull++;
}
results.Rows.Add(dr);
count++;
}
}
}
Console.WriteLine(count);
Console.WriteLine("Null");
Console.WriteLine(ctNull);
return results;
}
}
}
catch(NullReferenceException ex) {
Console.WriteLine("data error" + ex);
DataTable dt = new DataTable();
return dt;
}
}
上面的功能很好用!必须有一种方法可以检查最后一次登录是否超过 30 天。我将不胜感激任何帮助。谢谢你!
下面的答案是正确的谢谢
我必须添加以下代码才能将数据放入数据库:
if (de.Properties["LastLogonTimestamp"] != null && de.Properties["LastLogonTimestamp"].Count > 0)
{
Int64 lastLogonDateThisServer = new Int64();
IADsLargeInteger lgInt = (IADsLargeInteger)de.Properties["LastLogonTimestamp"].Value;
lastLogonDateThisServer = ((long)lgInt.HighPart << 32) + lgInt.LowPart;
dr["LastLogonTimestamp"] = DateTime.FromFileTime(lastLogonDateThisServer).ToString();
}
else
{
dr["LastLogonTimestamp"] = DateTime.MinValue.ToString();
ctNull++;
}
我将它放在 lastLogon 的下方 用于查询过滤器:我必须反转 < 以获取从现在到 30 天标记的数据。
string query = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(!userAccountControl:1.2.840.113556.1.4.803:=65536)(userAccountControl:1.2.840.113556.1.4.803:=262144)(userPrincipalName=1*@mil)(lastlogon>=" + DateTime.Now.AddDays(-90).ToFileTime() + ")(lastLogonTimestamp>=" + DateTime.Now.AddDays(-90).ToFileTime() + ")(mail=*))";