Extensibility Model - Extending UserPrincipal
My code:
var context = new PrincipalContext(
ContextType.Domain, "domain controller", "ou=foo,dc=bar,dc=com");
var userSearcher =
new PrincipalSearcher(new ActiveDirectoryUserPrincipal(context));
var results = userSearcher.FindAll();
Console.WriteLine(results.Count()); // outputs 2182
var regularUserSearcher = new PrincipalSearcher(new UserPrincipal(context));
var results2 = regularUserSearcher.FindAll();
Console.WriteLine(results2.Count()); // outputs 1579
var computerSearcher = new PrincipalSearcher(new ComputerPrincipal(context));
var results3 = computerSearcher.FindAll();
Console.WriteLine(results3.Count()); // outputs 603
[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("cn")]
public class ActiveDirectoryUserPrincipal : UserPrincipal
{
...
}
My custom principal returns computers and users. Anyone have a good suggestion on the best way to fix this? I know I could do this:
DirectoryProperty("objectCategory")]
public string ObjectCategory
{
get
{
var result = this.ExtensionGet("objectCateg开发者_开发知识库ory");
if (result == null || result.Length != 1)
return null;
else
return (string)result[0];
}
set
{
this.ExtensionSet("objectCategory", value);
}
}
and change the search to
var userSearchTemplate = new ActiveDirectoryUserPrincipal(context);
userSearchTemplate.ObjectCategory = "person";
var userSearcher = new PrincipalSearcher(userSearchTemplate);
var results = userSearcher.FindAll();
but it seems more like a hack than a solution...
精彩评论