以下是我的代码。我想检索网格中名字或姓氏相同的所有用户。但是这里得到了网格中的名称。
我正在让用户选择输入用户名。输入名称后,我应该可以在Active Directory中搜索并返回以用户输入的文本开头的所有用户。
我应该能够显示所有可能性,例如,如果用户输入adam
,我应该让他选择是否要查看adam josef
或adam john
e.t.c。
任何建议都会有所帮助。
这是代码
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 \项目\证书\证书\ WebForm4.aspx.cs:202
System.Web.UI.WebControls.Button.OnClick(EventArgs e)+118
System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串 eventArgument)+112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 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(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint) 5563
答案 0 :(得分:2)
您可以使用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();
不要对返回的所有数据进行两次迭代! (如在你的评论中 ).....
使用S.DS.AM课程更新#2:,始终获得完整的课程 - 您无能为力。如果要选择仅特定的LDAP属性,则需要使用原始方法。
通过这种方法,您需要确保在容器上创建DirectorySearcher
的根目录 - 例如单个AD对象(如特定用户)上的OU=Users
容器 - NOT 。
所以尝试使用此代码:
// 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();
这有用吗?
答案 1 :(得分:0)
deSearch.Filter = "(&(objectCategory=user)(givenName=*" + UserName + "*))";
工作正常