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

查看:118
本文介绍了检索用户的自定义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)

假设我想获取的属性是为prop1 prop2 ,什么我在做错了下面code:

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)

任何想法?

推荐答案

您确定您的属性设置?如果他们没有设置,他们是不是要被列为性质。

如果你正在寻找特定的属性,我建议你使用的 DirectorySearcher从

下面的例子是得到一个给定用户的公司财产。请注意,您验证属性存在第一,那么你解压。

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天全站免登陆