0

我正在为特定 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=*))";
4

1 回答 1

0

lastLogon在您的查询中,当您不需要它们时,前面有两个括号:

((lastLogon<=...

这就是您收到“搜索过滤器无效”错误的原因。那应该是:

(lastLogon<=...

但是您也在计算错误的值。你有这个:

new DateTime(DateTime.Now.AddDays( - 30).Ticks)

lastLogon不使用刻度。

稍后在您的代码中,您将使用FromFileTime回读lastLogon. 同样,您可以使用ToFileTime将日期转换为可在查询中使用的格式。像这样:

DateTime.Now.AddDays(-30).ToFileTime()

但请记住,lastLogon这不会复制 - 它仅在用户进行身份验证的最后一个域控制器上是准确的。因此,如果您的域有多个域控制器,您将无法获得大多数帐户的准确值。

lastLogonTimestamp正是出于这个原因创建了该属性。它至少每 2 周重复一次,因此您知道该值在 2 周内是准确的。您可以使用相同的格式:

string query = "(&(objectCategory=person)(objectClass=user)(lastLogonTimestamp<="
    + DateTime.Now.AddDays(-30).ToFileTime() + ")(mail=*))";

您可以在此处阅读有关该lastLogonTimestamp属性的更多信息:https ://blogs.technet.microsoft.com/askds/2009/04/15/the-lastlogontimestamp-attribute-what-it-was-design-for-and-how-it -作品/

于 2018-11-07T00:29:48.500 回答