係 Domain Controller 上獲得 Domain 上既 User Account 既 LDAP 資料
Default.aspx.cs |
using System.DirectoryServices; private void LDAPUserCheck(String HostName, String Query, String adminlogin, String adminpasswd) { DirectoryEntry objEntry = new DirectoryEntry(HostName, adminlogin, adminpasswd); DirectorySearcher objSearcher = new DirectorySearcher(objEntry, Query); SearchResultCollection objSearchResultCollection = objSearcher.FindAll(); DataTable dt = new DataTable(); dt.Columns.Add("cn"); dt.Columns.Add("userprincipalname"); dt.Columns.Add("adspath"); dt.Columns.Add("objectclass"); String ad = ""; String objectclass = ""; String UPN = ""; String cn = ""; if (objSearchResultCollection != null) { foreach (SearchResult iresult in objSearchResultCollection) { ad = iresult.Path; foreach (string PropertyName in iresult.Properties.PropertyNames) { foreach (Object key in iresult.GetDirectoryEntry().Properties[PropertyName]) { //Label2.Text += "User: " + PropertyName.ToLower() + ": " + key + "<BR />"; if (PropertyName.ToLower() == "objectclass") objectclass = objectclass.Trim() + " " + key; else if (PropertyName.ToLower() == "userprincipalname") UPN += key; else if (PropertyName.ToLower() == "cn") cn += key; } } //Label2.Text += "<br><br><br>"; dt.Rows.Add(cn, UPN, ad, objectclass); ad = ""; objectclass = ""; UPN = ""; cn = ""; } } //Label2.Text += "<br><br><br>"; GridView2.DataSource = dt; GridView2.DataBind(); } |