0

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

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

我应该能够显示所有可能性,例如,如果用户输入,adam我应该让他选择是否要查看adam josefadam 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): Unspecified error]
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) 在 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(String eventArgument) +112
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(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint) +5563

4

2 回答 2

2

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

// 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,它很好地展示了如何充分利用 .NET Framework 中的新功能System.DirectoryServices.AccountManagement。或查看System.DirectoryServices.AccountManagement命名空间上的 MSDN 文档。

当然,根据您的需要,您可能希望在您创建的“示例查询”用户主体上指定其他属性:

  • 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容器)上创建根目录,而不是在像特定用户这样的单个 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();

那样有用吗?

于 2012-11-08T08:46:50.520 回答
0
deSearch.Filter = "(&(objectCategory=user)(givenName=*" + UserName + "*))";

工作正常

于 2014-11-21T13:50:45.637 回答