【问题标题】:Retrieving custom Active Directory properties of users检索用户的自定义 Active Directory 属性
【发布时间】:2012-01-19 14:00:14
【问题描述】:

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

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

        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

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

有什么想法吗?

【问题讨论】:

标签: c# winforms active-directory ldap


【解决方案1】:

您确定您的属性已设置吗?如果未设置,它们将不会被列为属性。

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

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

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
}

【讨论】:

  • 感谢您的回答;值得一试!
【解决方案2】:

虽然不是直接回答您的问题,但我们使用以下内容:

        public static string GetProperty(string adUserId, string domain, string lDAPLoginId, string lDAPPassword, string propertyName)
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain, lDAPLoginId, lDAPPassword);
            UserPrincipal up = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, adUserId);

            string result = "";

            if (up != null)
            {
                result = PrincipalGetProperty(up, propertyName);
            }

            return result;
        }

【讨论】:

  • 这假设在某处定义了一个PrincipalGetProperty 方法,它不是内置的
猜你喜欢
  • 2018-03-30
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 1970-01-01
相关资源
最近更新 更多