2

C# で Active Directory にクエリを実行すると、奇妙な問題が発生します。

var ctx = new PrincipalContext(ContextType.Domain, "adr", "usr", "pwd");
var entry = new DirectoryEntry("LDAP://" + adr, usr, pwd);

var searcher = new DirectorySearcher(entry) { Filter = "(&(sAMAccountName=user_to_search))", PageSize = 2000 };

foreach (SearchResult searchUser in searcher.FindAll())
{
    // groups
    var groups = searchUser.GetPropertyValues("memberof");
}

var groups = UserPrincipal.FindByIdentity(ctx, "usr_to_search").GetGroups(ctx).ToList();

しかし、結果は同じではありません:

  • リターンズPrincipalSearcher14 グループ
  • リターンズDirectorySearcher12 グループ

さて、これはバグですか、それとも何か見逃したのでしょうか?

ありがとう

4

1 に答える 1

2

Oh my god, i have mistake in my extension method ( i < prop.count - 1).

 public static List<string> GetPropertyValues(this SearchResult searchResult,string property)
        {
            var prop = searchResult.Properties[property];
            var results = new List<string>();


            if (prop != null && prop.Count > 0)
            {
                for (int i = 0; i < prop.Count - 1; i++)
                {
                    results.Add(prop[i].ToString());
                }
            }
            return results;
        }

Sorry for stupid question.

于 2012-08-09T16:12:20.780 に答える