本文介绍了使用 System.DirectoryServices.AccountManagement 获取职位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地使用 AccountManagement 代码来检索基本的 AD 信息,但它只返回关于返回对象的非常有限的一组信息.如何使用 AccountManagement 功能从 AD 获取扩展信息.特别是在我的 AD 实例中似乎被称为职位的职位.

I've successfully used the AccountManagement code to retrieve basic AD information but it's only returning a very limited set of information about the returned object. How can I get extended information from AD using the AccountManagement functionality. Specifically the Job Title or title as it seems to be called in my instance of AD.

我知道如何使用旧的 DirectoryServices 来实现,但我想知道如何使用新的命名空间来实现.

I know how to do it using the older DirectoryServices but I'd like to know how to do it using the new namespace.

推荐答案

是的,UserPrincipal 上的默认属性集非常有限 - 但最重要的是:有一个简洁的可扩展性故事!

Yes, the default set of properties on UserPrincipal is quite limited - but the great part is: there's a neat extensibility story in place!

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

You need to define a class descending from UserPrincipal and then you can very easily get access to a lot more properties, if needed.

骨架看起来像这样:

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 方法允许您深入"底层目录条目并获取您可能感兴趣的所有属性......

And that's really almost all there is! The ExtensionGet and ExtensionSet methods allow you to "reach down" into the underlying directory entry and grab out all the attributes you might be interested in....

现在,在您的代码中,使用新的 UserPrincipalEx 类而不是 UserPrincipal:

Now, in your code, use your new UserPrincipalEx class instead of 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 命名空间及其扩展性故事的所有信息:

Read all about the System.DirectoryServices.AccountManagement namespace and its extensibility story here:

更新:抱歉 - 这是 UserPrincipalExSearchFilter 类 - 在原始帖子中错过了那个.如果需要,它只是显示了扩展搜索过滤器的能力:

Update: sorry - here's the UserPrincipalExSearchFilter class - missed that one in the original post. It just shows the ability to also extend the search filters, if need be:

public class UserPrincipalExSearchFilter : AdvancedFilters
{
    public UserPrincipalExSearchFilter(Principal p) : base(p) { }

    public void LogonCount(int value, MatchType mt)
    {
        this.AdvancedFilterSet("LogonCount", value, typeof(int), mt);
    }
}

这篇关于使用 System.DirectoryServices.AccountManagement 获取职位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 03:15