1

以下のコードを考えると

using (var context = new PrincipalContext(ContextType.Domain, SOME_DOMAIN))
using (UserPrincipal userPrincipal =  new UserPrincipal(context) { Enabled = true })
using (PrincipalSearchResult<Principal> results = new PrincipalSearcher(userPrincipal).FindAll())
{
    Console.WriteLine(results.Count());
}

using (var context = new PrincipalContext(ContextType.Domain, SOME_DOMAIN))
using (CustomUserPrinciple userPrincipal =  new CustomUserPrinciple(context) { Enabled = true })
using (PrincipalSearchResult<Principal> results = new PrincipalSearcher(userPrincipal).FindAll())
{
    Console.WriteLine(results.Count());
}

[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
public class CustomUserPrinciple : UserPrincipal
{
    public CustomUserPrinciple(PrincipalContext context)
        : base(context)
    {
    }
}

カウントは同じだと思っていましたが、カスタムプリンシパルを使用した検索では、最初の検索のようにユーザーだけが返されるわけではないようです。結果には、コンピューターなどの他のActiveDirectoryオブジェクトタイプが含まれます。

これは仕様によるものですか?そうであれば、カスタムプリンシパル検索を制限してユーザーのみを返す方法はありますか?

4

1 に答える 1

3

ADSIEDIT.MSC(W2K3サポートツール)などのツールを使用してオブジェクトを表示すると、コンピューターのobjectClassもユーザーであることがわかるため、結果にはコンピューターなどの他のActiveDirectoryオブジェクトタイプが含まれます。computerこれは、Active-Directoryのスキーマでは、クラスがクラスの子であるという事実によって説明されますuserobjectCategoryそれは違いを生むことを可能にする属性が存在します。

この方法でクラスを変更できます:

[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
public class CustomUserPrinciple : UserPrincipal
{
    public CustomUserPrinciple(PrincipalContext context)
        : base(context)
    {
    }

    [DirectoryProperty("objectCategory")]
    public string objectCategory
    {
      get
      {
        object[] result = this.ExtensionGet("objectCategory");
        if (result != null)
        {
          return (string)result[0];
        }
        else
        {
          return string.Empty;
        }
      }
      set { this.ExtensionSet("objectCategory", value); }
    }
}

そして、このようにクエリを管理します:

using (UserPrincipal userPrincipal =  new UserPrincipal(context) { objectCategory="Person",Enabled = true })
using (PrincipalSearchResult<Principal> results = new PrincipalSearcher(userPrincipal).FindAll())
{
    Console.WriteLine(results.Count());
}
于 2011-10-12T02:23:35.357 に答える