10

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

数人のユーザーしかいない場合に役立ちますが、AD には非常に多くのユーザーがいるため、クエリを実行すると

if ((String)(entry.Properties["sn"].Value) == "lname"
     && (String)(entry.Properties["givenName"].Value) == "fname")
{
    return entry.Properties["samAccountName"].Value.ToString();
}

完了するまでに時間がかかりすぎました。

特定のユーザー ログオン ID を姓名で検索するにはどうすればよいですか?

4

4 に答える 4

12

.NET 4 を使用しているため、System.DirectoryServices.AccountManagement(S.DS.AM) 名前空間を確認する必要があります。ここでそれについてすべて読んでください:

基本的に、ドメイン コンテキストを定義し、AD でユーザーやグループを簡単に見つけることができます。

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user - by e.g. his "samAccountName", or the Windows user name or something
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

if(user != null)
{
   // do something here....     
   string samAccountName = user.SamAccountName;
}

ユーザー名で指定されたユーザーが見つからない場合は、新しい検索機能を使用することもできます。

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the first name (GivenName) and a last name (Surname) 
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = firstName;
qbeUser.Surname = lastName;

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

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

新しい S.DS.AM を使用すると、AD でユーザーやグループを簡単に操作できます。また、1 人のユーザーを見つけるだけでも、比較的短時間で済みます。

于 2012-04-20T15:53:08.437 に答える
4

フィルタリングを行うには、AD サーバーを使用する必要があります。これを行うには、LDAP 構文フィルターを指定します。propertiesToLoadまた、 の引数を使用して、必要なプロパティのみを指定しますFindAll

    public static SearchResultCollection FindByName(
        string domain, string firstName, string lastName, string[] properties) {
        var rootEntry = new DirectoryEntry("LDAP://" + domain);
        var filter = string.Format("(&(sn={0})(givenName={1}))", lastName, firstName);
        var searcher = new DirectorySearcher(rootEntry, filter, properties);
        return searcher.FindAll();
    }

    // Using the method:
    var result = FindByName("mydomain", "Robert", "Smith", new[] { "samAccountName" })[0];
    string uName = (string)result.Properties["samAccountName"][0];
于 2012-04-20T16:03:04.180 に答える
1

でQueryFilterプロパティを設定し、代わりに をsearcher呼び出す必要があります。クエリ フィルターは、検索対象のフィールドを設定するUserPrincipalオブジェクトに設定できます。searcher.FindOne()searcher.FindAll()

Microsoft はQuery By Exampleページでこれの良い例を持っていますが、彼らの例は与えられた基準に一致するいくつかのオブジェクトを見つけることを期待しています.

リンクされた質問を要件に特に文字通り適応させると、

using (var context = new PrincipalContext(ContextType.Domain, "mydomain.com"))
{
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context) { GivenName = "fname", Surname = "lname" }))
    {
            foreach (var result in searcher.FindAll())
            {
                DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
                return de.Properties["samAccountName"].Value.ToString();
            }
    }
}
于 2012-04-20T15:42:30.860 に答える
0

entryがコレクションの一部である場合、次のIEnumerableようなことができます。

var entries = {do your population of the collection here}

var entry = entries.Where(e=>e.Properties["sn"].Value.ToString() == "lname"
    && e=>.Properties["givenName"].Value.ToString() == "fname")
    .FirstOrDefault();
于 2012-04-20T15:43:50.977 に答える