检索用户的自定义 Active Directory 属性 [英] Retrieving custom Active Directory properties of users

查看:29
本文介绍了检索用户的自定义 Active Directory 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试检索为我们的 Active Directory 用户设置的两个自定义属性,但似乎我一直在获取一个恒定的属性列表(在 2 个不同的 AD 服务器上测试)

I've been trying to retrieve two custom properties that are set on our Active Directory users, but it seems like I keep getting a constant list of Properties (tested over 2 different AD servers)

假设我试图获取的属性是 prop1prop2,我在下面的代码中做错了什么:

Assuming the properties I'm trying to fetch are prop1 and prop2, What am I doing wrong in the following code:

        List<String> nProps = new List<string>();

        DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://DOM");
        foreach (DirectoryEntry child in directoryEntry.Children)
        {
            // No filtering; ignore schemes that are not User schemes
            if (child.SchemaClassName == "User")
            {
                foreach (var sVar in child.Properties.PropertyNames)
                    nProps.Add(sVar.ToString());

                break;
            }
        }

nProps 不包含我的任何自定义属性(不是 prop1 也不是 prop2)

nProps does not contain ANY of my custom properties (not prop1 nor prop2)

(它确实包含其他属性,例如 BadPasswordAttempts、用户名等)

(it does contain other properties, like BadPasswordAttempts, Username, etc)

有什么想法吗?

推荐答案

你确定你的属性设置了吗?如果未设置它们,它们将不会被列为属性.

如果您正在寻找特定的属性,我建议您使用 目录搜索器

以下示例获取给定用户的公司属性.请注意,您首先验证该属性是否存在,然后提取它.

Are you sure your properties are set? if they are not set, they are not gonna be listed as properties.

If you're looking for specific properties, I'd recommend you to use DirectorySearcher

The following example is getting the company property of a given user. Note that you verify if the property exists first, then you extract it.

DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://DOM");

//Create a searcher on your DirectoryEntry
DirectorySearcher adSearch= new DirectorySearcher(directoryEntry);
adSearch.SearchScope = SearchScope.Subtree;    //Look into all subtree during the search
adSearch.Filter = "(&(ObjectClass=user)(sAMAccountName="+ username +"))";    //Filter information, here i'm looking at a user with given username
SearchResult sResul = adSearch.FindOne();       //username is unique, so I want to find only one

if (sResult.Properties.Contains("company"))     //Let's say I want the company name (any property here)
{
    string companyName = sResult.Properties["company"][0].ToString();    //Get the property info
}

这篇关于检索用户的自定义 Active Directory 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆