简体   繁体   English

如何查找没有值的 AD 属性的数据类型

[英]How do I find the data type of an AD attribute not having a value

I am struggling to find the data type of AD attributes that are not having a value already.我正在努力寻找没有值的 AD 属性的数据类型。 Thus far a resulting DirecoryEntry only contains a property for attributes that already have a value.到目前为止,生成的 DirecoryEntry 仅包含已具有值的属性的属性。 I can't find a method to obtain information about all other attributes.我找不到获取有关所有其他属性的信息的方法。

Adding a value to the 'PropertiesToLoad' doesn't seem to do anything.向“PropertiesToLoad”添加值似乎没有任何作用。 The returned DirectoryEntry object contains all attributes (with values) regardless of what is added here.无论此处添加了什么,返回的 DirectoryEntry 对象都包含所有属性(带值)。

Code used:使用的代码:

public void Test(string ldapPath)
{
   Type orgType;
   try
   {
     using (DirectoryEntry searchRoot = GetSearchRoot(ldapPath))
     {
        using (DirectorySearcher search = new DirectorySearcher(searchRoot))
        {
            search.Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=coen))";

            search.PropertiesToLoad.Add("msExchHomeServerName");

            SearchResult searchResult = search.FindOne();
            {
                using (DirectoryEntry entry = searchResult.GetDirectoryEntry())
                {
                    if (entry != null)
                    {
                        if (entry.Properties.Contains("msExchHomeServerName"))
                        {
                            orgType = entry.Properties["msExchHomeServerName"].GetType();
                        }
                        else // The value is empty and NOT present in the current 'entry.Properties' object.
                        {
                            throw new Exception("Unknown type");
                        }
                    }
                }
            }
        }
    }
   }
   catch (Exception e)
   {
     <handle exception>
   }
}

Is there a way to find the data type of the 'msExchHomeServerName' (or any of the 'empty' attributes) attribute?有没有办法找到“msExchHomeServerName”(或任何“空”属性)属性的数据类型?

Any help would be highly appreciated!任何帮助将不胜感激!

Coen科恩

Active Directory has a schema. Active Directory 有一个架构。 Obvious to say, not obvious to think about since, by default, you cannot explore it.显而易见,考虑起来并不明显,因为默认情况下,您无法探索它。

However, you can Register the Active Directory Schema MMC Snap-In 1 and then, in a fresh instance of MMC , add that Snap-In.但是,您可以注册 Active Directory 架构 MMC 管理单元1 ,然后在新的MMC实例中添加该管理单元。

This then allows you to explore the Classes and Attributes that make up your current AD schema (and add new classes/attributes if you know what you're doing and choose to do so).然后,这允许您探索构成当前 AD 模式的类和属性(如果您知道自己在做什么并选择这样做,则可以添加新的类/属性)。

msExchHomeServerName can then be discovered to be a "Unicode String", which means a plain string from C# should be acceptable.然后可以发现msExchHomeServerName是“Unicode 字符串”,这意味着应该可以接受来自 C# 的纯string Note also that some types (particularly numeric ones) may also specify Minimums and Maximums which should be observed.另请注意,某些类型(尤其是数字类型)也可能指定应遵守的最小值和最大值。

You can also explore the schema programatically via the ActiveDirectorySchema class , by eg calling ActiveDirectorySchema.GetCurrentSchema();您还可以通过ActiveDirectorySchema以编程方式探索模式,例如调用ActiveDirectorySchema.GetCurrentSchema(); and then explore from there.然后从那里探索。


1 I cannot remember if you need to have installed the general Domain Admin tools (such as Users and Computers) in order for that DLL to exist on your system. 1我不记得您是否需要安装一般的域管理工具(例如用户和计算机)才能使该 DLL 存在于您的系统中。

Thanks to Damien_The_Unbeliever who pointed me in the right direction, I managed to create the following method:感谢 Damien_The_Unbeliever 为我指明了正确的方向,我设法创建了以下方法:

public Dictionary<string, ActiveDirectorySyntax> GetAttributeSyntaxes(List<string> lstAttributeNames)
{
    Dictionary<string, ActiveDirectorySyntax> dictRes = new Dictionary<string, ActiveDirectorySyntax>();

    if (lstAttributeNames.Count > 0)
    {
        DirectoryContext directoryContext = new DirectoryContext(DirectoryContextType.DirectoryServer,
                                                             m_Server, m_UserName, m_Password);

        using (ActiveDirectorySchema currentSchema = ActiveDirectorySchema.GetSchema(directoryContext))
        {
            using (ActiveDirectorySchemaClass objClass = currentSchema.FindClass("user"))
            {
                if (objClass != null)
                {
                    ReadOnlyActiveDirectorySchemaPropertyCollection propcol = objClass.GetAllProperties();

                    foreach (ActiveDirectorySchemaProperty schemaProperty in propcol)
                    {
                        foreach (string attrName in lstAttributeNames)
                        {
                            if (schemaProperty.Name.Equals(attrName))
                            {                                    
                                dictRes.Add(attrName, schemaProperty.Syntax);

                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    return dictRes;
}

The returned 'schemaProperty.Syntax' contains sufficient information to determine the actual data type.返回的“schemaProperty.Syntax”包含足够的信息来确定实际数据类型。

Thanks Damien!谢谢达米安!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在XML文件中找到属性值的最大值? - How do I find the maximum value of an attribute value in an XML file? 使用 Microsoft.CodeAnalysis,如何获取 Type 类型的 Attribute 属性的值? - Using Microsoft.CodeAnalysis, how do I get the value of an Attribute property that is of type Type? 如何比较旧数据类型值和新数据类型值 - How do I compare the old data type value with the new ones 对于任何值类型T,如何确定对象的类型是否为IEnumerable <T>的子类? - How do I find out whether an object's type is a subclass of IEnumerable<T> for any value type T? 如何在C#中找到某些结构(值类型)或类(引用类型) - How do I find out something is a struct(value type) or a class(reference type) in C# 如何找到属性值 - How to find attribute value 如何从数字数据类型属性中检查字符串值? - How to check for string value from numeric data type attribute? 如何使用PrincipalSearchResult查找Group AD对象的grouptype属性<Principal> - How to find grouptype attribute of a Group AD object using PrincipalSearchResult<Principal> 当它是整数数据类型时,如何在gridview中找到Label值 - How can I find a Label value in my gridview when it is an integer data type 如何在XDocument的特定位置找到特定的XML属性 - How do I find a specific XML attribute in a specific place in an XDocument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM