可扩展性模型 - 扩展UserPrincipal

时间:2011-01-13 22:17:29

标签: c# .net-3.5 .net-4.0 directoryservices

我的代码:

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
{
  ...
}

我的自定义主体返回计算机用户。任何人都有一个很好的建议,以解决这个问题的最佳方法?我知道我可以这样做:

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

并将搜索更改为

var userSearchTemplate = new ActiveDirectoryUserPrincipal(context);
userSearchTemplate.ObjectCategory = "person";
var userSearcher = new PrincipalSearcher(userSearchTemplate);
var results = userSearcher.FindAll();

但它似乎更像是一个黑客而不是一个解决方案......

0 个答案:

没有答案