我想從使用LDAP的目錄服務器中顯示與用戶提供的名稱匹配的所有名稱,並將其綁定到網格視圖。我能夠實現這個任務,而不僅僅是一個名字,而是獲得像LDAP這樣的其他屬性:// CN = Neha Shetty,OU = Users,OU = MUM,OU = Mumbai,OU = India,OU = APAC,OU = bunt, DC = XXX,DC = COM。但我只是想要Neha Shetty。這裏是我的代碼從asp.net中獲取ldap的名稱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 + "*))";
deSearch = new DirectorySearcher("(&(objectCategory=user)(Name=*" + UserName + "*))");
//deSearch.SearchScope = SearchScope.Subtree;
string[] arrPropertiesToLoad = { "Surname" };
deSearch.PropertiesToLoad.AddRange(arrPropertiesToLoad);
// SearchResultCollection sResultColl = deSearch.FindAll();
SearchResultCollection sResultColl;
sResultColl = deSearch.FindAll();
Gridview1.DataSource = sResultColl;
Gridview1.DataBind();
我猜SearchResult到GridView的默認綁定將顯示條目的可分辨名稱。你應該從每個SearchResult中提取Surname屬性值(類似於sResultColl.Cast()。Select(sr => GetSurnameAttributValue).ToList();)並將結果字符串集合綁定到GridView。 –
jbl