2012-05-10 1 views
0

AD에있는 모든 컴퓨터와 현재 로그인 한 사용자 모두를 수신하려고합니다. "lastLogonStamp" 그러나 그것은 8 일 전에 내 서버가 AD에 로그인되었다는 잘못된 값을 반환합니다. 서버를 다시 시작한 경우에도 마찬가지입니다. 여기 또 다른 질문에서 코드를 가지고 : 당신이 .NET 3.5을 사용하는 경우모든 컴퓨터의 목록을 얻고 AD에 로그인 한 경우

How to list all computers and the last time they were logged onto in AD?

public DataTable GetListOfComputers(string domain, string userName, string password) 
    { 
     DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, 
       userName, password, AuthenticationTypes.Secure); 
     DirectorySearcher search = new DirectorySearcher(entry); 
     string query = "(objectclass=computer)"; 
     search.Filter = query; 

     search.PropertiesToLoad.Add("name"); 
     search.PropertiesToLoad.Add("lastLogonTimestamp"); 

     SearchResultCollection mySearchResultColl = search.FindAll(); 

     DataTable results = new DataTable(); 
     results.Columns.Add("name"); 
     results.Columns.Add("lastLogonTimestamp"); 

     foreach (SearchResult sr in mySearchResultColl) 
     { 
      DataRow dr = results.NewRow(); 
      DirectoryEntry de = sr.GetDirectoryEntry(); 
      dr["name"] = de.Properties["Name"].Value; 
      dr["lastLogonTimestamp"] = DateTime.FromFileTimeUtc(long.Parse(sr.Properties["lastLogonTimestamp"][0].ToString())); 
      results.Rows.Add(dr); 
      de.Close(); 
     } 

     return results; 
    } 
+0

빠른 응답 감사합니다. 자격 증명을 어디에서 전달합니까? 필자의 예에서는 IP 주소를 "관리자", "암호"라는 AD 서버에 전달했습니다. 어떻게 나 자신을 인증합니까? PS. 내 웹 서버는 광고가 아닙니다. 나는 외부 연결 만하고있다. DS. –

답변

0

를 최대, 당신이 할 수있는 PrincipalSearcher하고 "쿼리별로 예를 들어"주체를 사용할 수 있습니다 검색 :

// create your domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// define a "query-by-example" principal - here, we search for a ComputerPrincipal 
ComputerPrincipal qbeComputer = new ComputerPrincipal(ctx); 

// create your principal searcher passing in the QBE principal  
PrincipalSearcher srch = new PrincipalSearcher(qbeComputer); 

// find all matches 
foreach(var found in srch.FindAll()) 
{ 
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....   
    ComputerPrincipal cp = found as ComputerPrincipal; 

    if(cp != null) 
    { 
     string computerName = cp.Name; 
     DateTime lastLogon = cp.LastLogon; 
    } 
} 

을 아직하지 않은 경우 - 절대적으로 System.DirectoryServices.AccountManagement의 새로운 기능을 최대한 활용하는 방법을 잘 보여줍니다 MSDN 문서 Managing Directory Security Principals in the .NET Framework 3.5를 참조하십시오. 또는 MSDN documentation on the System.DirectoryServices.AccountManagement 네임 스페이스를 참조하십시오.

관련 문제