我已经成功地使用了AccountManagement代码来检索基本的AD信息,但是它只返回有关返回对象的非常有限的信息集。如何使用AccountManagement功能从AD获取扩展信息。具体来说,在我的AD实例中似乎称为“职务”或职务。

我知道如何使用旧的DirectoryServices来执行此操作,但是我想知道如何使用新的 namespace 来执行此操作。

最佳答案

是的,UserPrincipal的默认属性集非常有限-但很大的一部分是:有一个简洁的可扩展性故事!

您需要定义一个从UserPrincipal继承的类,然后,如果需要,您可以非常轻松地访问更多的属性。

骨架看起来像这样:

namespace ADExtended
{
    [DirectoryRdnPrefix("CN")]
    [DirectoryObjectClass("User")]
    public class UserPrincipalEx : UserPrincipal
    {
        // Inplement the constructor using the base class constructor.
        public UserPrincipalEx(PrincipalContext context) : base(context)
        { }

        // Implement the constructor with initialization parameters.
        public UserPrincipalEx(PrincipalContext context,
                             string samAccountName,
                             string password,
                             bool enabled) : base(context, samAccountName, password, enabled)
        {}

        UserPrincipalExSearchFilter searchFilter;

        new public UserPrincipalExSearchFilter AdvancedSearchFilter
        {
            get
            {
                if (null == searchFilter)
                    searchFilter = new UserPrincipalExSearchFilter(this);

                return searchFilter;
            }
        }

        // Create the "Title" property.
        [DirectoryProperty("title")]
        public string Title
        {
            get
            {
                if (ExtensionGet("title").Length != 1)
                    return string.Empty;

                return (string)ExtensionGet("title")[0];
            }
            set { ExtensionSet("title", value); }
        }

        // Implement the overloaded search method FindByIdentity.
        public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue)
        {
            return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue);
        }

        // Implement the overloaded search method FindByIdentity.
        public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
        {
            return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue);
        }
    }
}

这实际上几乎是全部! ExtensionGetExtensionSet方法允许您“进入”基础目录条目,并获取您可能感兴趣的所有属性。

现在,在您的代码中,使用新的UserPrincipalEx类而不是UserPrincipal:
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // Search the directory for the new object.
    UserPrincipalEx myUser = UserPrincipalEx.FindByIdentity(ctx, "someUserName");

    if(myUser != null)
    {
        // get the title which is now available on your "myUser" object!
        string title = myUser.Title;
    }
}

在此处阅读有关System.DirectoryServices.AccountManagement命名空间及其可扩展性故事的所有信息:
  • Managing Directory Security Principals in the .NET Framework 3.5

  • 更新:对不起-这是UserPrincipalExSearchFilter类-在原始帖子中错过了那个。它仅显示了还可以扩展搜索过滤器的功能,如果需要的话:
    public class UserPrincipalExSearchFilter : AdvancedFilters
    {
        public UserPrincipalExSearchFilter(Principal p) : base(p) { }
    
        public void LogonCount(int value, MatchType mt)
        {
            this.AdvancedFilterSet("LogonCount", value, typeof(int), mt);
        }
    }
    

    10-05 21:11
    查看更多