AdvancedFilters.LastLogonTime(DateTime, MatchType) Method

Definition

Sets the query filter for the LastLogon attribute to the specified DateTime and the specified comparison value.

public void LastLogonTime (DateTime logonTime, System.DirectoryServices.AccountManagement.MatchType match);

Parameters

logonTime
DateTime

A DateTime object.

match
MatchType

A MatchType that specifies how logonTime should be used in the comparison.

Examples

// Create the principal context for the usr object.
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "fabrikam.com", "CN=Users,DC=fabrikam,DC=com", "administrator", "SecurelyStoredPassword");

// Create the principal user object from the context
UserPrincipal usr = new UserPrincipal(ctx);

// Set the advanced query filter
usr.AdvancedSearchFilter.LastLogonTime(DateTime.Now.AddDays(-1), MatchType.GreaterThan);

// Create a PrincipalSearcher object.
PrincipalSearcher ps = new PrincipalSearcher(usr);
PrincipalSearchResult<Principal> results = ps.FindAll();

//Write the names of all users who have logged on since yesterday
foreach (UserPrincipal u in results)
{
	Console.WriteLine(u.Name);
}

Remarks

The time will default to UTC. If you want to specify the time in local time, then specify the Kind property of your DateTime object as DateTimeKind.Local.

Applies to

Product Versions
.NET 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided)
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

See also