繁体   English   中英

需要帮助在Active Directory中搜索多个属性

[英]need help searching for multiple properties in Active Directory

我正在对AD中的某个用户进行查询,并希望创建多个属性的列表。 下面的代码。 当我执行searchResult.Properties [“ manager”]或searchResult.Properties [“ mail”]时,我会以各种方式获得正确的结果。 但是我将如何搜索多个属性?

DirectoryEntry dEntry = new DirectoryEntry(path);    

                DirectorySearcher dSearcher = new DirectorySearcher(dEntry);                    

                dSearcher.Filter = "(&(ObjectClass=user)(samaccountname=mcavanaugh))";    

                sResults = dSearcher.FindAll();

                foreach (SearchResult searchResult in sResults)
                {
                    var sAMAccountName = searchResult.Properties["samaccountname"][0].ToString().ToLower();
                    if (sAMAccountName == "mcavanaugh")
                    {
                        //Right here is where i would select multiple ad properties
                        ResultPropertyValueCollection valueCollection = searchResult.Properties["manager, mail"];

                        foreach (Object propertyValue in valueCollection)
                        {
                            var PropertyName = (string)propertyValue.ToString();
                            testlist.Text = PropertyName;
                        }
                    }
                }

Properties属性没有一次访问多个属性的选项。 混合来自不同属性的值似乎并不明智。 最好的解决方案是两次运行foreach ,可能会创建一个要干燥的函数。

void addPropertyValues<T>(SearchResult sr, T testlist, string propName) {
    foreach (var pv in sr[propName]) {
        testlist.Text = pv.ToString();
    }
}

您可以在if

if (sAMAccountName == "mcavanaugh") {
    addPropertyValues(searchResult, testlist, "manager");
    addPropertyValues(searchResult, testlist, "mail");
}

我最近一直在努力。 我发现在哪里放置多个属性并将它们放入数组中,并将它们分配给属性。 到目前为止,效果一直不错。

DirectoryEntry myLdapConnection = new DirectoryEntry("LDAP://DC=demo,DC=Com");

DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = "(sAMAccountName=" + username + ")";

string[] requiredProperties = new string[] { "cn", "Displayname", "Title", "Department", "l", "Homedirectory", "telephoneNumber", "lockoutTime", "badlogoncount", "passwordexpired", "badPasswordTime", "whenCreated", "sAMAccountName", "pwdLastSet", "thumbnailPhoto", "givenName", "sn", "mail", "msRTCSIP-PrimaryUserAddress", "distinguishedName", "manager" };


foreach(String property in requiredProperties)
search.PropertiesToLoad.Add(property);

//next code will output to a usertextbox that I had set up in a Form. You can convert to console.writeline

if (searchResult != null) {
    foreach(String property in requiredProperties)
    foreach(Object myCollection in searchResult.Properties[property])
    UserTextbox.Text += "\r\n" + (String.Format("{0,0} : {1} : {2}", property, myCollection.ToString(), myCollection.GetType().ToString()));
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM