[英]Find all users in Ldap using asp.net c#
以下是我的代碼。 我想檢索網格中名字或姓氏相同的所有用戶。 但是這里得到了網格中的名稱。
我正在為用戶提供輸入用戶名的選擇。 輸入名稱后,我應該可以在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
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.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(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+5563
您可以使用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文章.NET Framework 3.5中的管理目錄安全主體,它很好地展示了如何充分利用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
的根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();
那樣有用嗎?
deSearch.Filter = "(&(objectCategory=user)(givenName=*" + UserName + "*))";
工作正常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.