none
C#中如何利用DirectoryService获取域帐号的扩展属性? RRS feed

  • 问题

  • DirectoryEntry AD = new DirectoryEntry("LDAP://" + textBox5.Text, textBox1.Text, textBox2.Text, AuthenticationTypes.Secure);

    DirectorySearcher ADFind = new DirectorySearcher(AD);

                String str = String.Format("(&(&(objectCategory=user)(samAccountName={0})))", textBox3.Text);

                ADFind.Filter = str;

                SearchResult results = ADFind.FindOne();

                if (results != null)

                {

                     DirectoryEntry chm = results.GetDirectoryEntry();

                    foreach (PropertyValueCollection pvc in chm.Properties)

                    {

                        textBox4.Text += pvc.PropertyName + ":" + pvc.Value + "\r\n";

                        if(typeof(byte[])== pvc.Value.GetType())

                        {

                            textBox4.Text += Convert.ToBase64String((byte[])pvc.Value);

                        }

                    }

                }

    这段代码获取不到扩展属性,请问如何访问图中扩展属性和值?



    2015年5月28日 9:21

答案

  • 问题已解决,是域服务器上需要做一些配置,同时如果扩展属性没有赋值的话,连属性也是获取不到的。

    谢谢你提供的指导!

    2015年6月8日 8:12

全部回复

  • 你好:

    请参考一下MSDN文档:

    https://msdn.microsoft.com/zh-cn/library/ms180859(v=vs.80).aspx

    尝试一下通过DirectoryEntry.Properties["property-name"]这种方式来读取。参考以下代码:

    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
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年5月29日 8:54
    版主
  • 按照您提供的这种方式也不行,属性就在选项卡中可以看到,但是就是不能够通过程序获取!

    如下图:

    2015年6月8日 6:48
  • 问题已解决,是域服务器上需要做一些配置,同时如果扩展属性没有赋值的话,连属性也是获取不到的。

    谢谢你提供的指导!

    2015年6月8日 8:12