3

Active Directory からユーザーのリストを取得するにはどうすればよいですか?

上記ページをご覧ください。ほとんどの質問に答えてくれましたが、コンピューターの最終ログオン時刻を取得しようとすると問題が発生します。そのようなオプションが見つからなかったので、まったく新しい質問をする代わりに、そのページにコメントする方法があれば申し訳ありません。

using (var context = new PrincipalContext(ContextType.Domain, "cat.pcsb.org"))
        {
            using (var searcher = new PrincipalSearcher(new ComputerPrincipal(context)))
            {
                foreach (var result in searcher.FindAll())
                {
                    DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
                    Console.WriteLine("Name: " + de.Properties["name"].Value);
                    Console.WriteLine("Last Logon Time: " + de.Properties["lastLogon"].Value);
                    Console.WriteLine();
                }
            }
        }
        Console.ReadLine();

UserPrincipal を ComputerPrincipal に置き換えました。名前とその他のいくつかのプロパティは正常に機能しますが、ログオンは機能しません。DateTime にキャストするなど、さまざまなことを試みましたが (キャストは失敗しました)、何も機能しませんでした。上記の結果は、System.__ComObject になります。では、最終ログオン時刻を正しく取得するにはどうすればよいでしょうか?

4

2 に答える 2

8

ComputerPrincipal によって返される LastLogon プロパティを使用しないのはなぜですか? (ComputerPrincipal は、AuthenicatablePrincipal です)

using (var context = new PrincipalContext(ContextType.Domain, "cat.pcsb.org"))
{
    using (var searcher = new PrincipalSearcher(new ComputerPrincipal(context)))
    {
        foreach (var result in searcher.FindAll())
        {
            var auth = result as AuthenticablePrincipal;
            if(auth != null)
            {
                Console.WriteLine("Name: " + auth.Name);
                Console.WriteLine("Last Logon Time: " + auth.LastLogon);
                Console.WriteLine();
            }
        }
    }
}
Console.ReadLine();

LastLogon はレプリケートされたプロパティではないことに注意してください。そのため、複数のドメイン コントローラーがある場合は、各コントローラーにクエリを実行し、最新の結果を提供したユーザーを見つける必要があります。

于 2013-10-18T16:54:47.883 に答える
4

すべてのドメイン コントローラを反復処理して、最新のログオン時刻を見つける必要があります。

以下のコードは、ユーザーの最終ログオン時刻を見つけます。

public DateTime findlastlogon(string userName)

    {
        DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, "domainName");
        DateTime latestLogon = DateTime.MinValue;
        DomainControllerCollection dcc = DomainController.FindAll(context);
        Parallel.ForEach(dcc.Cast<object>(), dc1 =>
                {


                    DirectorySearcher ds;
                    DomainController dc = (DomainController)dc1;
                    using (ds = dc.GetDirectorySearcher())
                    {
                        try
                        {
                            ds.Filter = String.Format(
                              "(sAMAccountName={0})",
                              userName
                              );
                            ds.PropertiesToLoad.Add("lastLogon");
                            ds.SizeLimit = 1;
                            SearchResult sr = ds.FindOne();

                            if (sr != null)
                            {
                                DateTime lastLogon = DateTime.MinValue;
                                if (sr.Properties.Contains("lastLogon"))
                                {
                                    lastLogon = DateTime.FromFileTime(
                                      (long)sr.Properties["lastLogon"][0]
                                      );
                                }

                                if (DateTime.Compare(lastLogon, latestLogon) > 0)
                                {
                                    latestLogon = lastLogon;
                                    //servername = dc1.Name;
                                }
                            }
                        }
                        catch (Exception)
                        {

                        }                          
                    }
                });
        return latestLogon;
    }

コンピュータの最終ログオン時刻を取得するには、sAMAccountName を Name に置き換えます。

ds.Filter = String.Format( "(Name={0})", userName );

于 2013-10-25T09:03:39.670 に答える