C# Webform中读取Windows AD/LDAP域用户清单
2017-02-08 15:31
274 查看
直接上干货,核心代码如下,读取出来相应的用户清单到DataTable中。需要其它字段可以自己增加,别忘了引用using System.DirectoryServices。
我还增加了LDAP(Windows AD域)用户一键导入的功能,截图如下,代码核心就是循环读取上述获取的DataTable,记得判断一下用户是否已经存在即可。
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,记得判断一下用户是否已经存在即可。
相关文章推荐
- [C#(WebForm)] - 解决了防止用户重复登陆和session超时
- 如何读取从ACCESS中读取所有用户表名(C#)
- 如何读取从ACCESS中读取所有用户表名(C#)
- C# 读取域用户
- c#读取xml文件配置文件Winform及WebForm-Demo具体解释
- c#读取xml文件配置文件Winform及WebForm-Demo详解
- C# 连接ACCESS 验证用户登录,读取SESSION值
- C#读取Windows AD用户列表
- C#如何从文本文件读取信息并使用ArrayList类向用户显示该信息
- C#读取注册表,获取本机安装的软件清单
- [ZT]C# 代码实现设置用户"NETWORK SERVICE"具有对文件夹的读取权限。
- 使用c#如何读取xml文件
- .Net/C# 应用程序直接读取本地 Cookies 文件 (由于 WinXP SP2 调用 API: InternetGetCookie 无果)
- 把AD中存在的用户启动为LCS用户(C#)
- C#第2个程序,读取偶网站的数据!
- ASP.NET中在线用户统计(C#)
- 设置Windows系统NTFS某个目录的用户访问权限(c#)
- C#中使用XML——读取XML
- .net中webform和winform连接sql server 2000数据库的c#操作类
- C#中使用网络函数 (第一部分 用户函数)[翻译]