[英]Bind data from LDAP to asp.repeater control
我有一个转发器控件,我希望用LDAP中的数据填充它。 我可以连接到Active Directory,它将返回600个名称的列表,但是当我尝试绑定数据时,它将返回以下消息:
System.DirectoryServices.SearchResult'不包含名称为'name'的属性
我也不想只返回姓名,而是电子邮件,电话,职位和其他一些字段。
有办法吗?
这是我的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());
}
}
这是转发器的代码:
<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>
自从我使用此库已有一段时间了,但我认为某些属性甚至都没有映射。 无论如何,如果您只是想获取全名,请尝试使用属性[“ displayName”]
这是一个关于可用于propertiesToLoad的属性的问题的链接。 答案之一包含指向属性列表的链接。 该列表很长,因此我没有全部检查,但没有通过快速搜索找到它。
至于返回多个属性,只需像在另一个属性之上执行以下操作那样加载更多属性即可:
ds.PropertiesToLoad.Add("sAMAccountName");
ds.PropertiesToLoad.Add("displayName");
ds.PropertiesToLoad.Add("userPrincipalName");
ds.PropertiesToLoad.Add("objectSid");
并以相同的方式获取它们的值:
sr.Properties["sAMAccountName"][0].ToString()
sr.Properties["displayName"][0].ToString()
sr.Properties["userPrincipalName"][0].ToString()
sr.Properties["objectSid"][0].ToString()
另类
我建议,如果可以的话,请开始使用System.Management.Automation库从AD获取任何内容。 我进行了更改,这使我的生活变得更加简单,因为您使用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.