简体   繁体   English

将数据从LDAP绑定到asp.repeater控件

[英]Bind data from LDAP to asp.repeater control

I have a repeater control and I wish to populate it with data from LDAP. 我有一个转发器控件,我希望用LDAP中的数据填充它。 I can connect to Active Directory and it will return a list of 600 plus names, but when I try and bind the data it returns the following message: 我可以连接到Active Directory,它将返回600个名称的列表,但是当我尝试绑定数据时,它将返回以下消息:

System.DirectoryServices.SearchResult' does not contain a property with the name 'name' System.DirectoryServices.SearchResult'不包含名称为'name'的属性

I also dont want to just return the name, but email, phone, jobtitle and a few other fields. 我也不想只返回姓名,而是电子邮件,电话,职位和其他一些字段。

Is there a way I can do this? 有办法吗?

This is my C# code: 这是我的C#代码:

        private void GetAllUsers()
    {
        SearchResultCollection results;
        DirectorySearcher ds = null;
        DirectoryEntry de = new DirectoryEntry(GetCurrentDomainPath());
        ds = new DirectorySearcher(de);
        ds.PropertiesToLoad.Add("name");
        ds.Filter = "(&(objectCategory=User)(objectClass=person))";
        results = ds.FindAll();
        rptAD.DataSource = results;
        rptAD.DataBind();
        foreach (SearchResult sr in results)
        {
            Debug.WriteLine(sr.Properties["name"][0].ToString());
        }
    }

and this is the code for the repeater: 这是转发器的代码:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="container">
    <h2>AD Users</h2>
    <asp:Repeater ID="rptAD" runat="server">
        <HeaderTemplate>
            <table class="table table-striped table-bordered">
                <tr>
                    <td>AD User</td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
                <tr>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem,"name") %>;
                    </td>
                </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
</div>

It´s been a while since I used this library but I think some of the properties are not even mapped. 自从我使用此库已有一段时间了,但我认为某些属性甚至都没有映射。 Anyways if you are just trying to get the full name try the property ["displayName"] 无论如何,如果您只是想获取全名,请尝试使用属性[“ displayName”]

Here is link to a question regarding which properties are available for propertiesToLoad. 是一个关于可用于propertiesToLoad的属性的问题的链接。 One of the answers contains links to the list of attributes. 答案之一包含指向属性列表的链接。 The list is long so I didn´t check it all but I didn´t find it with a quick search. 该列表很长,因此我没有全部检查,但没有通过快速搜索找到它。

As for returning more than one attribute just load more properties like you did above one below the other like this: 至于返回多个属性,只需像在另一个属性之上执行以下操作那样加载更多属性即可:

ds.PropertiesToLoad.Add("sAMAccountName");
ds.PropertiesToLoad.Add("displayName");
ds.PropertiesToLoad.Add("userPrincipalName");
ds.PropertiesToLoad.Add("objectSid");

and obtaining their values the same way as well: 并以相同的方式获取它们的值:

sr.Properties["sAMAccountName"][0].ToString()
sr.Properties["displayName"][0].ToString()
sr.Properties["userPrincipalName"][0].ToString()
sr.Properties["objectSid"][0].ToString()

Alternative 另类

I would suggest though that if you can, start using the System.Management.Automation library for obtaining anything from AD. 我建议,如果可以的话,请开始使用System.Management.Automation库从AD获取任何内容。 I changed, and it made my life much simpler just because you execute your code with powershell commands. 我进行了更改,这使我的生活变得更加简单,因为您使用powershell命令执行了代码。

using (var powerShell = PowerShell.Create())
            {
                var adUsersList = powerShell
                     .AddCommand("Get-ADUser")
                     .AddParameter("LDAPFilter", ldapQuery)
                     .AddParameter("Properties", new string[] { "EmailAddress", "LockedOut", "Created", "Modified" })
                     .Invoke();
            }

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

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