【问题标题】:Find all users in Ldap using asp.net c#使用asp.net c#查找Ldap中的所有用户
【发布时间】:2012-11-08 07:55:43
【问题描述】:

下面是我的代码。我想检索网格中名字或姓氏相同的所有用户。但这里是在网格中对名称本身进行排序。

我让用户可以选择输入用户名。一旦他输入名称,我应该能够在 Active Directory 中搜索并返回以用户输入的文本开头的所有用户。

我应该能够显示所有可能性,例如,如果用户输入 adam,我应该让他选择是要查看 adam josef 还是 adam john

任何建议都会有所帮助。

这里是代码

       DirectoryEntry de = new DirectoryEntry("ADConnection");

        DirectorySearcher deSearch = new DirectorySearcher(de);

        //set the search filter    
        deSearch.SearchRoot = de;
        String UserName = txt_To.Text;
        deSearch.Filter = "(&(objectCategory=user)(GivenName=*" + UserName + "*))";
        string[] arrPropertiesToLoad = { "sn" };
        deSearch.PropertiesToLoad.AddRange(arrPropertiesToLoad);

      SearchResultCollection sResultColl = deSearch.FindAll();//Getting undefined error



        Gridview1.DataSource = sResultColl ;
        Gridview1.DataBind();

这是堆栈跟踪

[COMException (0x80004005): 未指定的错误]
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +439513
System.DirectoryServices.DirectoryEntry.Bind() +36
System.DirectoryServices.DirectoryEntry.get_AdsObject() +31
System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) +78
System.DirectoryServices.DirectorySearcher.FindAll() +9
Certificate.WebForm4.btngo0_Click(Object sender, EventArgs e) in C:\Users\273714\documents\visual studio 2010\Projects\Certificate\Certificate\WebForm4.aspx.cs:202
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串 事件参数)+112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串 eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint) +5563

【问题讨论】:

  • 如果我给的用户名是 sam,在网格中我得到它在垂直线上的 S A M。如果我取消注释这些行会出现未定义的异常。
  • 未定义异常是什么意思?你能显示这个异常和调用堆栈吗?
  • 堆栈跟踪存在问题。我通过取消注释这些行来更改代码。我哪里错了? :(
  • 调用堆栈-> Certificate.DLL!Certificate.WebForm4.btngo0_Click(object sender, System.EventArgs e) Line 202 + 0xa bytes C#

标签: c# active-directory ldap


【解决方案1】:

您可以使用PrincipalSearcher 和“示例查询”主体进行搜索:

// create your domain context
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 = "*" + UserName + "*";

// 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 的“示例查询”。

更新:如果你想找到一堆用户并将它们绑定到一个gridview,使用这个代码:

// create your domain context
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 = "*" + UserName + "*";

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

var results = srch.FindAll();

Gridview1.DataSource = results; 
Gridview1.DataBind();

不要对返回的所有数据进行两次迭代! (如你的评论 ) .....

更新 #2: 使用 S.DS.AM 课程,您总是获得完整课程 - 您对此无能为力。如果你想只选择特定的 LDAP 属性,你需要使用你原来的方法。

通过这种方法,您需要确保在 容器 上创建 DirectorySearcher 的根 - 例如OU=Users 容器 - NOT 位于单个 AD 对象上,例如特定用户。

所以尝试使用这段代码:

// define a *CONTAINER* as the root of your searcher!
DirectoryEntry de = new DirectoryEntry("LDAP://OU=Users,OU=NJY,OU=NewJersey,OU=USA,OU=NorthAmerica,OU=America,OU=gunt,DC=xxx,DC=com");

DirectorySearcher deSearch = new DirectorySearcher(de);

// set the search filter    
string UserName = txt_To.Text;

deSearch.Filter = string.Format("(&(objectCategory=user)(givenName=*{0}*))", UserName);
deSearch.PropertiesToLoad.Add("sn");

SearchResultCollection sResultColl = deSearch.FindAll();

Gridview1.DataSource = sResultColl;
Gridview1.DataBind();

这行得通吗?

【讨论】:

  • 我想我得到了我想要的..但​​是我怎样才能将结果绑定到网格。我使用了 Gridview1.DataSource = found; Gridview1.DataBind();但出现错误
  • 它需要很长时间。有没有办法减少时间,比如只显示名字和姓氏或类似的东西。
  • foreach (var found in srch.FindAll()) { string result = srch.FindAll().ToString(); Gridview1.DataSource = 结果; Gridview1.DataBind();// do // 在这里做任何事情 - “found”是“Principal”类型 - 它可以是用户、组、计算机..... } 这就是我使用的方式
  • @user1665707:难怪很慢!您正在遍历找到的所有用户,然后您再次执行.FindAll() - 对于每个用户......查看我的更新
  • 哦,好吧,那么将就此事与有关人员交谈。谢谢你的帮助。
【解决方案2】:
deSearch.Filter = "(&(objectCategory=user)(givenName=*" + UserName + "*))";

工作正常

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多