1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
/// <summary>
/// Liefert ein Dictionary der letzten Anmeldung eines Users.
/// Da das Attribut nicht auf alle DC's repliziert wird, werden alle DC's durchsucht.
/// </summary>
/// <param name="User">Der NT-Account (cn)</param>
/// <example>
/// System.Collections.Generic.Dictionary<string, DateTime> UserLastLogon = GetUserLastLogon(Environment.UserName);
/// foreach (KeyValuePair<string, DateTime> kvp in UserLastLogon)
/// {
/// string ServerName = kvp.Key;
/// DateTime LastLogon = kvp.Value;
/// }
/// </example>
public System.Collections.Generic.Dictionary<string, DateTime> GetUserLastLogon(string User)
{
DateTime DateLastLogon = DateTime.MinValue;
string LDAPServerName = "";
System.Collections.Generic.Dictionary<string, DateTime> LastLogon = new System.Collections.Generic.Dictionary<string, DateTime>();
System.DirectoryServices.ActiveDirectory.Domain Domain = System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain();
foreach (System.DirectoryServices.ActiveDirectory.DomainController Server in Domain.DomainControllers)
{
object LastLogonLongTime = GetLDAPValue(User, "lastlogon", "user", "LDAP://" + Server.Name);
if (LastLogonLongTime != null)
{
if (System.DateTime.FromFileTime((long)LastLogonLongTime) > DateLastLogon)
{
DateLastLogon = System.DateTime.FromFileTime((long)LastLogonLongTime);
LDAPServerName = Server.Name;
}
}
}
if (LDAPServerName == "")
{
LastLogon.Add("", DateTime.MaxValue);
return LastLogon;
}
else
{
LastLogon.Add(LDAPServerName, DateLastLogon);
return LastLogon;
}
}
|