C# Webform中读取Windows AD/LDAP域用户清单
直接上干货,核心代码如下,读取出来相应的用户清单到DataTable中。需要其它字段可以自己增加,别忘了引用using System.DirectoryServices。
1 #region private DataTable GetData(int pageIndex, int pageSize, out int recordCount) 获取数据 2 3 /// <summary> 4 /// 获取数据 5 /// </summary> 6 private DataTable GetData(int pageIndex, int pageSize, out int recordCount) 7 { 8 string ldapPath = this.txtLDAPPath.Text.Trim(); 9 string ldapDomain = this.txtLDAPDomain.Text.Trim(); 10 string ldapUserName = this.txtLDAPUserName.Text.Trim(); 11 string ldapPassword = this.txtLDAPPassword.Text.Trim(); 12 string searchValue = this.txtSearch.Text.Trim(); 13 14 //总记录数量 15 int iRecordCount = 0; 16 //BaseUserManager manager = new BaseUserManager(this.UserCenterDbHelper, this.UserInfo); 17 //自定义LDAP用户表 18 DataTable dtLdapUser = null; 19 dtLdapUser = new DataTable(); 20 DataColumn[] columns = 21 { 22 new DataColumn(BaseUserEntity.FieldId), 23 new DataColumn(BaseUserEntity.FieldUserName), 24 new DataColumn(BaseUserEntity.FieldRealName), 25 new DataColumn(BaseUserEntity.FieldUserFrom) 26 }; 27 dtLdapUser.Columns.AddRange(columns); 28 try 29 { 30 // 读取用户 31 using (var de = new DirectoryEntry()) 32 { 33 de.Path = ldapPath; 34 if (!string.IsNullOrEmpty(ldapUserName)) 35 { 36 if (string.IsNullOrEmpty(ldapDomain)) 37 { 38 de.Username = ldapUserName; 39 } 40 else 41 { 42 de.Username = ldapDomain + "\\" + ldapUserName; 43 } 44 } 45 //密码为空就默认不验证用户密码 46 if (!string.IsNullOrEmpty(ldapPassword)) 47 { 48 de.Password = ldapPassword; 49 de.AuthenticationType = AuthenticationTypes.Secure; 50 } 51 //刷新缓存 52 de.RefreshCache(); 53 54 using (DirectorySearcher searcher = new DirectorySearcher()) 55 { 56 searcher.SearchRoot = de; 57 searcher.Filter = "(objectClass=user)"; 58 searcher.SearchScope = SearchScope.Subtree; 59 60 //需要导入的属性 61 //登录名 62 searcher.PropertiesToLoad.Add("userprincipalname"); 63 //姓名 64 searcher.PropertiesToLoad.Add("name"); 65 66 //定义排序方式 67 searcher.Sort = new SortOption("givenName", System.DirectoryServices.SortDirection.Ascending); 68 69 SearchResultCollection results = searcher.FindAll(); 70 int i = 0; 71 foreach (SearchResult result in results) 72 { 73 DataRow row = dtLdapUser.NewRow(); 74 75 ResultPropertyCollection props = result.Properties; 76 string[] userNameArray = null; 77 if (props.PropertyNames != null) 78 { 79 foreach (string propName in props.PropertyNames) 80 { 81 if (propName == "userprincipalname") 82 { 83 userNameArray = props[propName][0].ToString().Split('@'); 84 //只保留@前的用户名 85 row[BaseUserEntity.FieldUserName] = userNameArray[0].ToString(); 86 } 87 if (propName == "name") 88 { 89 row[BaseUserEntity.FieldRealName] = props[propName][0]; 90 } 91 } 92 } 93 row[BaseUserEntity.FieldUserFrom] = "AD"; 94 if (userNameArray != null && !string.IsNullOrEmpty(userNameArray[0].ToString())) 95 { 96 i++; 97 row[BaseUserEntity.FieldId] = i; 98 dtLdapUser.Rows.Add(row); 99 } 100 } 101 } 102 } 103 104 recordCount = dtLdapUser.Rows.Count; 105 106 } 107 catch (Exception e) 108 { 109 recordCount = 0; 110 } 111 return dtLdapUser; 112 } 113 #endregion
我还增加了LDAP(Windows AD域)用户一键导入的功能,截图如下,代码核心就是循环读取上述获取的DataTable,记得判断一下用户是否已经存在即可。
专注、持续、微创新
站在巨人的肩膀上
方向不对,努力白费