1

次のコードを使用してグローバル カタログを検索します。

public SearchResultCollection SearchGlobalCatalog(string username)
{
    var de = new DirectoryEntry("GC://SERVERNAME", "USERNAME", "PASSWORD");
    var ds = new DirectorySearcher(de);
    ds.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=" + username + "*))";
    ds.SearchScope = SearchScope.Subtree;                
    var searchResults = ds.FindAll();

    return searchResults;
}

UserPrincipalさて、問題はからオブジェクトのリストを取得する方法がわからないことSearchResultCollectionです。これを行う理由は、従業員 ID など、グローバル カタログでは使用できない一部のユーザー プロパティにアクセスするためです。

4

1 に答える 1

2

AUserPrincipalSystem.DirectoryServices.AccountManagement名前空間の一部です。

オブジェクトを取得するには、その名前空間のヘルパー クラスを使用しUserPrincipalます。

UserPrincipal次のような試しを使用せずに:

using (var userBinding = new DirectoryEntry("LDAP://domain.forest.company.com"))
{
    using (DirectorySearcher adSearch = new DirectorySearcher(userBinding))
    {
        adSearch.ReferralChasing = ReferralChasingOption.All;
        adSearch.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=" + username + "*))";
        adSearch.PropertiesToLoad.Add("employeeID");
        adSearch.PropertiesToLoad.Add("givenname");
        adSearch.PropertiesToLoad.Add("samaccountname");

        var result = adSearch.FindOne();

        var employeeId = result.Properties["employeeID"][0].ToString();
    }
}
于 2013-12-19T07:12:30.090 に答える