プロパティが設定されていない AD LDS (ADAM) のインスタンスでユーザーを検索しようとしています。たとえば、「会社」プロパティが ADAM ストア (または AD) の値に設定されていません。
カスタムオブジェクトでPrincipalSearcher
および カスタムを使用すると、次のエラーが発生します。UserPrincipal
AdvancedSearchFilters
An unhandled exception of type 'System.ArgumentException' occurred in System.DirectoryServices.dll Additional information: The (&(objectClass=user)(!(company=))) search filter is invalid.
ここに私のサンプルコードがあります:
using System;
using System.DirectoryServices.AccountManagement;
using System.Security.Permissions;
using System.Linq;
namespace AdamDump
{
class Program
{
static void Main(string[] args)
{
PrincipalContext context = new PrincipalContext(ContextType.ApplicationDirectory, "MyAdamInstance:50000", "OU=Adam Users,dc=apps01,dc=mydomain", "queryaccount", "password");
// initialize a Query By Example
using (MyUserPrincipal myUserPrincipal = new MyUserPrincipal(context))
{
myUserPrincipal.MyAdvancedFilters.WhereCompanyNotSet();
PrincipalSearchResult<Principal> principals = null;
// do the search...
using (PrincipalSearcher principalSearcher = new PrincipalSearcher(myUserPrincipal))
{
principals = principalSearcher.FindAll();
}
var myUsers = principals.Select(principal => principal as MyUserPrincipal).ToList();
foreach (var user in myUsers)
Console.WriteLine("Name: {0}, Account{1}", user.DisplayName, user.SamAccountName);
Console.WriteLine("Total found: {0}", myUsers.Count);
}
}
}
[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
public class MyUserPrincipal : UserPrincipal
{
private MyAdvancedFilters _myAdvancedFilters;
/// <summary>
/// Initializes a new instance of the <see cref="MyUserPrincipal"/> class.
/// </summary>
/// <param name="context">A <see cref="PrincipalContext"/> to associate this instance with.</param>
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
public MyUserPrincipal(PrincipalContext context)
: base(context) { }
public MyAdvancedFilters MyAdvancedFilters
{
get
{
return this.AdvancedSearchFilter as MyAdvancedFilters;
}
}
public override AdvancedFilters AdvancedSearchFilter
{
get
{
if (_myAdvancedFilters == null)
{
_myAdvancedFilters = new MyAdvancedFilters(this);
}
return _myAdvancedFilters;
}
}
}
public class MyAdvancedFilters : AdvancedFilters
{
/// <summary>
/// Initializes a new instance of the <see cref="MyAdvancedFilters"/> class.
/// </summary>
/// <param name="principal">The source <see cref="Principal"/></param>
public MyAdvancedFilters(Principal principal) : base(principal) { }
public void WhereCompanyNotSet()
{
this.AdvancedFilterSet("company", "", typeof(string), MatchType.NotEquals);
}
}
}