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

问题描述

我已经成功地使用了AccountManagement code检索基本广告信息,但它只是返回一组关于返回的对象的信息非常有限。我怎样才能从AD使用AccountManagement功能扩展信息。具体的职位或头衔,因为它好像是叫于公元我的实例。

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);
        }
    }
}

这就是真的,几乎所有有!该 ExtensionGet ExtensionSet 方法让你到达下到下面的目录项,并抓住了所有的属性你可能感兴趣的......

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

现在,在code,使用 UserPrincipal 的新 UserPrincipalEx 类代替:

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