简体   繁体   English

使用asp.net c#查找Ldap中的所有用户

[英]Find all users in Ldap using asp.net c#

Below is my code. 以下是我的代码。 I want to retrieve all the users whose first name or last name is same in a grid. 我想检索网格中名字或姓氏相同的所有用户。 But here am getting the name itself in the grid sorted. 但是这里得到了网格中的名称。

I'm giving user a choice to enter user name. 我正在为用户提供输入用户名的选择。 Once he enter the name I should be able to search in Active Directory and return all user starting with that text entered by the user. 输入名称后,我应该可以在Active Directory中搜索并返回以用户输入的文本开头的所有用户。

I should be able to display all possibilities, for example if user enters adam I should give him choice to select whether he want to see adam josef or adam john etc 我应该能够显示所有可能性,例如,如果用户输入adam我应该选择他是否要查看adam josefadam john

Any suggestion will be helpful. 任何建议都会有所帮助。

Here is the code 这是代码

       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();

Here is the stack trace 这是堆栈跟踪

[COMException (0x80004005): Unspecified error] [COMException(0x80004005):未指定错误]
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +439513 System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)+439513
System.DirectoryServices.DirectoryEntry.Bind() +36 System.DirectoryServices.DirectoryEntry.Bind()+36
System.DirectoryServices.DirectoryEntry.get_AdsObject() +31 System.DirectoryServices.DirectoryEntry.get_AdsObject()+31
System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) +78 System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)+78
System.DirectoryServices.DirectorySearcher.FindAll() +9 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 C:\\ Users \\ 273714 \\ documents \\ visual studio 2010 \\ Projects \\ Certificate \\ Certificate \\ WebForm4.aspx.cs中的Certificate.WebForm4.btngo0_Click(Object sender,EventArgs e):202
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebControls.Button.OnClick(EventArgs e)+118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)+112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)+10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+5563

You can use a PrincipalSearcher and a "query-by-example" principal to do your searching: 您可以使用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.....          
}

If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement . 如果您还没有 - 绝对阅读MSDN文章.NET Framework 3.5中的管理目录安全主体,它很好地展示了如何充分利用System.DirectoryServices.AccountManagement中的新功能。 Or see the MSDN documentation on the System.DirectoryServices.AccountManagement namespace. 或者,请参阅System.DirectoryServices.AccountManagement命名空间MSDN文档

Of course, depending on your need, you might want to specify other properties on that "query-by-example" user principal you create: 当然,根据您的需要,您可能希望在您创建的“按示例查询”用户主体上指定其他属性:

  • DisplayName (typically: first name + space + last name) DisplayName (通常为:名字+空格+姓氏)
  • SAM Account Name - your Windows/AD account name SAM Account Name - 您的Windows / AD帐户名称
  • User Principal Name - your "username@yourcompany.com" style name User Principal Name - 您的“username@yourcompany.com”样式名称

You can specify any of the properties on the UserPrincipal and use those as "query-by-example" for your PrincipalSearcher . 您可以在UserPrincipal上指定任何属性,并将这些属性用作PrincipalSearcher “按示例查询”。

Update: if you want to find a bunch of users and bind those to a gridview, use this code: 更新:如果要查找一组用户并将其绑定到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();

Don't iterate twice over all the data returned! 不要在返回的所有数据上迭代两次! (as in your comment ) ..... (如你的评论).....

Update #2: with the S.DS.AM classes, you always get the complete class - there's nothing you can do about this. 更新#2:使用S.DS.AM课程,你总能得到完整的课程 - 你无能为力。 If you want to select only specific LDAP attributes, you need to use your original approach. 如果您想选择只有特定的LDAP属性,您需要使用原始的方法。

From that approach, you need to make sure to create the root of the DirectorySearcher on a container - eg the OU=Users container - NOT on an individual AD object like a specific user. 从这种方法来看,您需要确保在容器上创建DirectorySearcher的根DirectorySearcher - 例如OU=Users容器 - 而不是像特定用户那样在单个AD对象上。

So try using this code: 所以尝试使用此代码:

// 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();

Does that work? 那样有用吗?

deSearch.Filter = "(&(objectCategory=user)(givenName=*" + UserName + "*))";

工作正常

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

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