我想使用Active Directory为我们公司创建一个目录Intranet网站。到目前为止我得到了这个,但是当我在调试模式下运行时,代码在searchResultCollection....search.findAll();
中显示:
[DirectoryServicesCOMException(0x80072020):发生操作错误。]
我已尝试将IIS asp.net模拟更改为已启用,但我收到HTTP错误500.24。 我的用户名具有对Active Directory的读访问权限。是否有一些我缺少的东西,或者有人指出我正确的方向。我到处都看,这是我被卡住了。
提前感谢您的帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.DirectoryServices;
using System.Web.Security;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
GetADUsers();
}
public void GetADUsers()
{
DirectoryEntry myLdap = new DirectoryEntry("LDAP://OU=Nix,DC=systems,DC=com");
DirectorySearcher search = new DirectorySearcher(myLdap);
search.CacheResults = true;
search.SearchScope = SearchScope.Subtree;
search.Filter = "(objectlass=person)";
SearchResultCollection allResults = search.FindAll();
foreach (SearchResult sr in allResults)
{
Response.Write(sr.Properties["name"].ToString());
}
}
答案 0 :(得分:1)
您可以使用PrincipalSearcher
和"按示例查询"负责你的搜索:
// create your domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// define a "query-by-example" principal - here, we search for a UserPrincipal
// and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Bruce";
qbeUser.Surname = "Miller";
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach(var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
}
}
如果您还没有 - 绝对阅读MSDN文章Managing Directory Security Principals in the .NET Framework 3.5,该文章很好地展示了如何充分利用System.DirectoryServices.AccountManagement
中的新功能。或者查看MSDN documentation on the System.DirectoryServices.AccountManagement命名空间。
当然,根据您的需要,您可能希望在其上指定其他属性"按示例查询"您创建的用户主体:
DisplayName
(通常:名字+空格+姓氏)SAM Account Name
- 您的Windows / AD帐户名称User Principal Name
- 您的" username@yourcompany.com"样式名称您可以在UserPrincipal
上指定任何属性,并将其用作"按示例查询"为您的PrincipalSearcher
。
答案 1 :(得分:0)
自我重新启动后,我再次测试它运行没有错误然后添加其余代码以在gridview中显示。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
GetADUsers();
}
public void GetADUsers()
{
DirectoryEntry myLdap = new DirectoryEntry("LDAP://OU=Nix,DC=systems,DC=com");
DirectorySearcher search = new DirectorySearcher(myLdap);
search.CacheResults = true;
search.SearchScope = SearchScope.Subtree;
search.Filter = "(objectlass=person)";
SearchResultCollection allResults = search.FindAll();
search.PropertiesToLoad.Add("samaccountname");
Grid1.DataSource = allResults;
Grid1.DataBind();
}