zoukankan      html  css  js  c++  java
  • AD Usre lastLogon Time

    Problem with getting lastlogon for account - C#.NET / AD

    Hi guys!
    I have a code:

    1. StringLastlogon(string username,string domain)
    2. {
    3. try
    4. {
    5. {
    6. DirectoryContext context =newDirectoryContext(DirectoryContextType.Domain,domain);
    7. DateTime latestLogon =DateTime.MinValue;
    8. string servername =null;
    9. DomainControllerCollection dcc =DomainController.FindAll(context);
    10. foreach(DomainController dc in dcc)
    11. {
    12. DirectorySearcher ds;
    13. using(dc)
    14. using(ds = dc.GetDirectorySearcher())
    15. {
    16. ds.Filter=String.Format("(sAMAccountName={0})",username);
    17. ds.PropertiesToLoad.Add("lastLogon");
    18. ds.SizeLimit=1;
    19. SearchResult sr = ds.FindOne();
    20. if(sr !=null)
    21. {
    22. DateTimelastLogon=DateTime.MinValue;
    23. if(sr.Properties.Contains("lastLogon"))
    24. {
    25. lastLogon=DateTime.FromFileTime((long)sr.Properties["lastLogon"][0]);
    26. }
    27. if(DateTime.Compare(lastLogon, latestLogon)>0)
    28. {
    29. latestLogon =lastLogon;
    30. servername = dc.Name;
    31. }
    32. }
    33. }
    34. }
    35. return latestLogon.ToString();
    36. }
    37. }
    38. catch(Exception)
    39. {
    40. err =true;
    41. returnnull;
    42. }
    43. }
  • 相关阅读:
    我的第一篇博客
    汇编语言——基础知识 Cop
    汇编语言第二章实验 Cop
    什么是<!DOCTYPE html>
    python第三方模块安装的几个方式
    linux下安装memcacheQ
    python正则表达式
    边框背景
    伪类
    属性选择符和样式
  • 原文地址:https://www.cnblogs.com/siri/p/2736519.html
Copyright © 2011-2022 走看看